<!Doctype html>
<html xmlns=http://www.w3.org/1999/xhtml>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <meta property="qc:admins" content="465267610762567726375" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />  
	<title>JSP 教程 | 菜鸟教程</title>

  <link rel='dns-prefetch' href='//s.w.org'>
<link rel="canonical" href="http://www.runoob.com/jsp/jsp-tutorial.html" />
<meta name="keywords" content="JSP 教程,jsp">
<meta name="description" content="JSP 教程   JSP与PHP、ASP、ASP.NET等语言类似，运行在服务端的语言。 JSP（全称Java Server Pages）是由Sun Microsystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求，而动态生成HTML、XML或其他格式文档的Web网页的技术标准。 JSP技术是以Java语言作为脚本语言的，JSP网页为整个服务器端的Java库单元提供了一个接口来服务于HTTP的应用程序。 JSP..">
		
	<link rel="shortcut icon" href="http://www.runoob.com/favicon.ico" mce_href="http://www.runoob.com/favicon.ico" type="image/x-icon" >
	<link rel="stylesheet" href="http://www.runoob.com/wp-content/themes/runoob/style.css?v=1.02" type="text/css" media="all" />	
	<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/fontawesome/4.2.0/css/font-awesome.min.css" media="all" />	
  <script src="http://cdn.static.runoob.com/libs/jquery/1.8.1/jquery.min.js"></script>
  <!--[if lt IE 9]>
     <script src="http://cdn.static.runoob.com/libs/html5shiv/3.7/html5shiv.min.js"></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="http://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link"> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-intro.html" rel="next"> JSP 简介</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">教程</span>
</h1>
<div class="tutintro">
<p>JSP与PHP、ASP、ASP.NET等语言类似，运行在服务端的语言。</p>
<p>JSP（全称Java Server Pages）是由Sun Microsystems公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求，而动态生成HTML、XML或其他格式文档的Web网页的技术标准。</p>
<p>JSP技术是以Java语言作为脚本语言的，JSP网页为整个服务器端的Java库单元提供了一个接口来服务于HTTP的应用程序。</p>
<p>JSP文件后缀名为(*.jsp)。</p>
<p>JSP开发的WEB应用可以跨平台使用，既可以运行在Linux上也能运行在Window上。
</div>
<hr>
<h2>第一个JSP程序</h2>
<p>语言学习入门的第一个程序一般都是输出"Hello World！"，JSP输出"Hello World！"代码如下所示：</p>
<pre>
&lt;html&gt;
    &lt;head&gt;
           &lt;title&gt;第一个JSP程序&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
           &lt;%
                  out.println("Hello World！");
           %&gt;
    &lt;/body&gt;
&lt;/html&gt;
</pre>
<hr><h2>
开始学习JSP</h2>
<p>了解了JSP的基本概念后，现在让我们<a href="/jsp/jsp-intro.html" title="jsp简介" target="_blank">开始来学习JSP</a>吧。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link"> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-intro.html" rel="next"> JSP 简介</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<form action="index.php">
<input class="placeholder" value="搜索……" name="s" autocomplete="off">
</form>
</div>
<div class="row">
<div class="col logo">
<h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
</div>
<div class="col right-list">
<button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
</div>
<div class="col search search-desktop last">
<form action="http://www.runoob.com/" target="_blank">
<input class="placeholder" id="s" name="s" value="搜索……" autocomplete="off">
</form>
</div>
</div>
</div>
<div class="container navigation">
<div class="row">
<div class="col nav">
<ul class="pc-nav">
<li><a href="http://www.runoob.com/">首页</a></li>
<li><a href="/html/html-tutorial.html">HTML</a></li>
<li><a href="/css/css-tutorial.html">CSS</a></li>
<li><a href="/js/js-tutorial.html">JavaScript</a></li>
<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
<li><a href="/sql/sql-tutorial.html">SQL</a></li>
<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
<li><a href="/php/php-tutorial.html">PHP</a></li>
<li><a href="/python/python-tutorial.html">Python</a></li>
<li><a href="/cprogramming/c-tutorial.html">C</a></li>
<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
<li><a href="/java/java-tutorial.html">Java</a></li>
<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
<li><a href="/sitemap">更多……</a></li>
</ul>
<ul class="mobile-nav">
<li><a href="http://www.runoob.com/">首页</a></li>
<li><a href="/html/html-tutorial.html">HTML</a></li>
<li><a href="/css/css-tutorial.html">CSS</a></li>
<li><a href="/js/js-tutorial.html">JavaScript</a></li>
<a href="javascript:void(0)" class="search-reveal">Search</a>
</ul>
</div>
</div>
</div>
<div class="container main">
<div class="row">
<div class="col left-column">
<div class="tab">JSP 教程</div>
<div class="sidebar-box gallery-list">
<div class="design" id="leftcolumn">
<a target="_top" title="JSP 教程" href="/jsp/jsp-tutorial.html">
JSP 教程 </a>
<a target="_top" title="JSP 简介" href="/jsp/jsp-intro.html">
JSP 简介 </a>
<a target="_top" title="JSP 开发环境搭建" href="/jsp/jsp-setup.html">
JSP 开发环境搭建 </a>
<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a> <a target="_top" title="JSP 结构" href="/jsp/jsp-architecture.html">
JSP 结构 </a>
<a target="_top" title="JSP 生命周期" href="/jsp/jsp-life-cycle.html">
JSP 生命周期 </a>
<a target="_top" title="JSP 语法" href="/jsp/jsp-syntax.html">
JSP 语法 </a>
<a target="_top" title="JSP 指令" href="/jsp/jsp-directives.html">
JSP 指令 </a>
<a target="_top" title="JSP 动作元素" href="/jsp/jsp-actions.html">
JSP 动作元素 </a>
<a target="_top" title="JSP 隐式对象" href="/jsp/jsp-implicit-objects.html">
JSP 隐式对象 </a>
<a target="_top" title="JSP 客户端请求" href="/jsp/jsp-client-request.html">
JSP 客户端请求 </a>
<a target="_top" title="JSP 服务器响应" href="/jsp/jsp-server-response.html">
JSP 服务器响应 </a>
<a target="_top" title="JSP HTTP 状态码" href="/jsp/jsp-http-status-codes.html">
JSP HTTP 状态码 </a>
<a target="_top" title="JSP 表单处理" href="/jsp/jsp-form-processing.html">
JSP 表单处理 </a>
<a target="_top" title="JSP 过滤器" href="/jsp/jsp-writing-filters.html">
JSP 过滤器 </a>
<a target="_top" title="JSP Cookie 处理" href="/jsp/jsp-cookies.html">
JSP Cookie 处理 </a>
<a target="_top" title="JSP Session" href="/jsp/jsp-session.html">
JSP Session </a>
<a target="_top" title="JSP 文件上传" href="/jsp/jsp-file-uploading.html">
JSP 文件上传 </a>
<a target="_top" title="JSP 日期处理" href="/jsp/jsp-handling-date.html">
JSP 日期处理 </a>
<a target="_top" title="JSP 页面重定向" href="/jsp/jsp-page-redirect.html">
JSP 页面重定向 </a>
<a target="_top" title="JSP 点击量统计" href="/jsp/jsp-hits-counter.html">
JSP 点击量统计 </a>
<a target="_top" title="JSP 自动刷新" href="/jsp/jsp-auto-refresh.html">
JSP 自动刷新 </a>
<a target="_top" title="JSP 发送邮件" href="/jsp/jsp-sending-email.html">
JSP 发送邮件 </a>
<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2> <a target="_top" title="JSP 标准标签库（JSTL）" href="/jsp/jsp-jstl.html">
JSP 标准标签库（JSTL） </a>
<a target="_top" title="JSP 连接数据库" href="/jsp/jsp-database-access.html">
JSP 连接数据库 </a>
<a target="_top" title="JSP XML 数据处理" href="/jsp/jsp-xml-data.html">
JSP XML 数据处理 </a>
<a target="_top" title="JSP JavaBean" href="/jsp/jsp-javabean.html">
JSP JavaBean </a>
<a target="_top" title="JSP 自定义标签" href="/jsp/jsp-custom-tags.html">
JSP 自定义标签 </a>
<a target="_top" title="JSP 表达式语言" href="/jsp/jsp-expression-language.html">
JSP 表达式语言 </a>
<a target="_top" title="JSP 异常处理" href="/jsp/jsp-exception-handling.html">
JSP 异常处理 </a>
<a target="_top" title="JSP 调试" href="/jsp/jsp-debugging.html">
JSP 调试 </a>
<a target="_top" title="JSP 国际化" href="/jsp/jsp-internationalization.html">
JSP 国际化 </a>
</div>
</div>
</div> <div class="col middle-column">
<div class="article">
<div class="article-heading-ad">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px" data-ad-client="ca-pub-5751451760833794" data-ad-slot="1691338467" data-ad-format="horizontal"></ins>
<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
</div>
<div class="previous-next-links">
<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-tutorial.html" rel="prev"> JSP 教程</a> </div>
<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-setup.html" rel="next"> JSP 开发环境搭建</a> →</div>
</div>
<div class="article-body">
<div class="article-intro" id="content">
<h1>JSP <span class="color_h1">简介</span>
</h1>
<h2>什么是Java Server Pages?</h2>
<p>
JSP全称Java Server Pages，是一种动态网页开发技术。它使用JSP标签在HTML网页中插入Java代码。标签通常以&lt;%开头以%&gt;结束。
</p><p>
JSP是一种Java servlet，主要用于实现Java web应用程序的用户界面部分。网页开发者们通过结合HTML代码、XHTML代码、XML元素以及嵌入JSP操作和命令来编写JSP。
</p><p>
JSP通过网页表单获取用户输入数据、访问数据库及其他数据源，然后动态地创建网页。
</p><p>
JSP标签有多种功能，比如访问数据库、记录用户选择信息、访问JavaBeans组件等，还可以在不同的网页中传递控制信息和共享信息。
</p><p>
<hr><h2>
为什么使用JSP？
</h2>
<p>
JSP程序与CGI程序有着相似的功能，但和CGI程序相比，JSP程序有如下优势：</p>
<ul>
<li>
性能更加优越，因为JSP可以直接在HTML网页中动态嵌入元素而不需要单独引用CGI文件。</li><li>
服务器调用的是已经编译好的JSP文件，而不像CGI/Perl那样必须先载入解释器和目标脚本。</li><li>
JSP基于Java Servlets API，因此，JSP拥有各种强大的企业级Java API，包括JDBC，JNDI，EJB，JAXP等等。</li><li>
JSP页面可以与处理业务逻辑的servlets一起使用，这种模式被Java servlet 模板引擎所支持。</li></ul>
<p>
最后，JSP是Java EE不可或缺的一部分，是一个完整的企业级应用平台。这意味着JSP可以用最简单的方式来实现最复杂的应用。
</p>
<hr>
<h2>
JSP的优势
</h2>
<p>以下列出了使用JSP带来的其他好处：</p>
<ul><li>
与ASP相比：JSP有两大优势。首先，动态部分用Java编写，而不是VB或其他MS专用语言，所以更加强大与易用。第二点就是JSP易于移植到非MS平台上。</li><li>
与纯 Servlets相比：JSP可以很方便的编写或者修改HTML网页而不用去面对大量的println语句。</li><li>
与SSI相比：SSI无法使用表单数据、无法进行数据库链接。</li><li>
与JavaScript相比：虽然JavaScript可以在客户端动态生成HTML，但是很难与服务器交互，因此不能提供复杂的服务，比如访问数据库和图像处理等等。</li><li>
与静态HTML相比：静态HTML不包含动态信息。</li></ul>
<hr>
<h2>
接下来呢？
</h2>
<p>
我们将会带您一步一步地来搭建JSP运行环境，这需要有一定的Java基础。
</p>
<p>
如果您还未学过Java，可以先学习我们为您提供的<a href="/java/java-tutorial.html">Java教程</a>。
</p>
</div>
</div>
<div class="previous-next-links">
<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-tutorial.html" rel="prev"> JSP 教程</a> </div>
<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-setup.html" rel="next"> JSP 开发环境搭建</a> →</div>
</div>
<div class="sidebar-box ad-box ad-box-large">
<div class="ad-336280">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px" data-ad-client="ca-pub-5751451760833794" data-ad-slot="1691338467" data-ad-format="rectangle"></ins>
<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
</div>
</div>
</div>
</div>
<div class="fivecol last right-column">
<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
<div class="sidebar-box">
<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
<div class="qqinfo">
</div>
</div>
<div class="tab tab-light-blue">分类导航</div>
<div class="sidebar-box sidebar-cate">
<div class="sidebar-tree">
<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul> </div>
</div>
<br>
<div class="sidebar-box ad-box ad-box-large">
<div class="sidebar-box advertise-here">
<a href="javascript:void(0);">Advertisement</a>
</div>
<div class="ad-600160">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle" style="display:inline-block;width:160px;height:600px" data-ad-client="ca-pub-5751451760833794" data-ad-slot="4106274865"></ins>
<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
</div>
</div>
</div></div>
</div>
<div id="footer" class="mar-t50">
<div class="w3cschoolcc-block">
<div class="w3cschoolcc cf">
<dl>
<dt>
在线实例
</dt>
<dd>
&middot;<a href="/html/html-examples.html">HTML 实例</a>
</dd>
<dd>
&middot;<a href="/css/css-examples.html">CSS 实例</a>
</dd>
<dd>
&middot;<a href="/js/js-examples.html">JavaScript 实例</a>
</dd>
<dd>
&middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
</dd>
<dd>
&middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
</dd>
<dd>
&middot;<a href="/xml/xml-examples.html">XML 实例</a>
</dd>
<dd>
&middot;<a href="/java/java-examples.html">Java 实例</a>
</dd>
</dl>
<dl>
<dt>
字符集&工具
</dt>
<dd>
&middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
</dd>
<dd>
&middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
</dd>
<dd>
&middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
</dd>
<dd>
&middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
</dd>
<dd>
&middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
</dd>
<dd>
&middot; <a href="/jsontool">JSON 格式化工具</a>
</dd>
</dl>
<dl>
<dt>
最新更新
</dt>
<dd>
&middot;
<a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
</dd>
<dd>
&middot;
<a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type() ...</a>
</dd>
<dd>
&middot;
<a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim() ...</a>
</dd>
<dd>
&middot;
<a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
</dd>
<dd>
&middot;
<a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
</dd>
<dd>
&middot;
<a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
</dd>
<dd>
&middot;
<a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
</dd>
</dl>
<dl>
<dt>
站点信息
</dt>
<dd>
&middot;
<a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
</dd>
<dd>
&middot;
<a href="/disclaimer">免责声明</a>
</dd>
<dd>
&middot;
<a href="/aboutus">关于我们</a>
</dd>
<dd>
&middot;
<a href="/archives">文章归档</a>
</dd>
</dl>
<div class="search-share">
<div class="app-download">
<div>
<strong>关注微信</strong>
</div>
</div>
<div class="share">
<img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
</div>
</div>
</div>
</div>
<div class="w-1000 copyright">
Copyright &copy; 2013-2016 <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
<strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
</div>
</div>
<div class="fixed-btn">
<a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
<a class="qrcode" href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
<a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
<div id="bottom-qrcode" class="modal panel-modal hide fade in">
<h4>微信关注</h4>
<div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
</div>
</div>
<div style="display:none;">
<script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
<script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-intro.html" rel="prev"> JSP 简介</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-architecture.html" rel="next"> JSP 结构</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">开发环境搭建</span>
</h1>

<p>
JSP开发环境是您用来开发、测试和运行JSP程序的地方。</p><p>
本节将会带您搭建JSP开发环境，具体包括以下几个步骤。</p>
<p>
如果你使用的是 Eclipse 环境，可以直接参阅：<a href="http://www.runoob.com/jsp/eclipse-jsp.html">Eclipse JSP/Servlet 环境搭建</a>。</p>

<hr>
<h2>
配置Java开发工具（JDK）
</h2>
<p>
这一步涉及Java SDK的下载和PATH环境变量的配置。
</p><p>
您可以从Oracle公司的Java页面中下载SDK：<a rel="nofollow" href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" target="_blank">Java SE Downloads</a>
</p>
<p>
Java SDK下载完后，请按照给定的指示来安装和配置SDK。最后，通过设置PATH和JAVA_HOME环境变量来指明包括java和javac的文件夹路径，通常是java_install_dir/bin和java_install_dir。
</p><p>
假如您用的是Windows系统并且SDK的安装目录为C::\jdk1.5.0_20，那么您就需要在 C:\autoexec.bat 文件中添加以下两行：
</p>
<pre>
set PATH=C:\jdk1.5.0_20\bin;%PATH%
set JAVA_HOME=C:\jdk1.5.0_20
</pre>
<p>
或者，在Windows NT/2000/XP下，您可以直接右击我的电脑图标，选择属性，然后高级，然后环境变量，接下来您就可以很方便地设置PATH变量并且确定退出就行了。
</p>
<p>
在Linux/Unix系统下，如果SDK的安装目录为/usr/local/jdk1.5.0_20并且使用的是C shell，那么您就需要在.cshrc文件中添加以下两行：
</p>
<pre>
setenv PATH /usr/local/jdk1.5.0_20/bin:$PATH
setenv JAVA_HOME /usr/local/jdk1.5.0_20
</pre>
<p>
或者，假如您正在使用类似于Borland JBuilder、Eclipse、IntelliJ IDEA和Sun ONE Studio这样的集成开发环境，可以试着编译并运行一个简单的程序来确定IDE（集成开发环境）是否已经知道 SDK的安装目录。
</p>
<p>本步骤你也可以参考本站<a href="/java/java-environment-setup.html" target="_blank">Java开发环境配置</a>章节的教程。</p>
<hr>
<h2>设置Web服务器：Tomcat
</h2>
<p>
目前，市场上有很多支持JSP和Servlets开发的Web服务器。他们中的一些可以免费下载和使用，Tomcat就是其中之一。
</p>
<p>
Apache Tomcat是一个开源软件，可作为独立的服务器来运行JSP和Servlets，也可以集成在 Apache Web Server中。以下是Tomcat的配置方法：
</p>
<ul><li>
下载最新版本的Tomcat：<a rel="nofollow" href="http://tomcat.apache.org/" target="_blank">http://tomcat.apache.org/</a>。</li><li>
<li>
下载完安装文件后，将压缩文件解压到一个方便的地方，比如Windows下的C:\apache-tomcat-5.5.29目录或者Linux/Unix下的/usr/local/apache-tomcat-5.5.29目录，然后创建CATALINA_HOME环境变量指向这些目录。
</li></ul>
<p>
在Windows机器下，Tomcat可以通过执行以下命令来启动：
</p>
<pre>
%CATALINA_HOME%\bin\startup.bat
或者
C:\apache-tomcat-5.5.29\bin\startup.bat
</pre>
<p>
在Linux/Unix机器下，Tomcat可以通过执行以下命令来启动：
</p>
<pre>
$CATALINA_HOME/bin/startup.sh
或者
/usr/local/apache-tomcat-5.5.29/bin/startup.sh
</pre>
<p>成功启动Tomcat后，通过访问http://localhost:8080/便可以使用Tomcat自带的一些web应用了。假如一切顺利的话，您应该能够看到以下的页面：
</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/TomcatHomePage.jpg"/>
</p>
<p>
更多关于配置和运行Tomcat的信息可以在Tomcat提供的文档中找到，或者去Tomcat官网查阅：http://tomcat.apache.org。
</p>
<p>
在Windows机器下，Tomcat可以通过执行以下命令来停止：
</p>
<pre>
%CATALINA_HOME%\bin\shutdown
或者
C:\apache-tomcat-5.5.29\bin\shutdown
</pre>
<p>
在Linux/Unix机器下，Tomcat可以通过执行以下命令来停止：
</p>
<pre>
$CATALINA_HOME/bin/shutdown.sh
或者
/usr/local/apache-tomcat-5.5.29/bin/shutdown.sh
</pre><hr><h2>
设置CLASSPATH环境变量
</h2>
<p>
由于servlets不是Java SE的一部分，所以您必须标示出servlet类的编译器。
</p><p>
假如您用的是Windows机器，您需要在C:\autoexec.bat文件中添加以下两行：
</p>
<pre>
set CATALINA=C:\apache-tomcat-5.5.29
set CLASSPATH=%CATALINA%\common\lib\jsp-api.jar;%CLASSPATH%
</pre>
<p>
或者，在Windows NT/2000/XP下，您只要右击我的电脑，选择属性，然后点击高级，然后点击环境变量，接下来便可以设置CLASSPATH变量并且确定退出即可。
</p><p>
在Linux/Unix机器下，假如您使用的是C shell，那么您就需要在.cshrc文件中添加以下两行：
</p>
<pre>
setenv CATALINA=/usr/local/apache-tomcat-5.5.29
setenv CLASSPATH $CATALINA/common/lib/jsp-api.jar:$CLASSPATH
</pre>
<p>
注意：如果您的开发路径是C:\JSPDev (Windows)或者 /usr/JSPDev (Linux/Unix)，那么您就需要将这些路径添加进CLASSPATH变量中。
</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-intro.html" rel="prev"> JSP 简介</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-architecture.html" rel="next"> JSP 结构</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isplainobject.html" title="jQuery.isPlainObject()   方法">jQuery.isPlainO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isnumeric.html" title="jQuery.isNumeric()  方法">jQuery.isNumeri...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isfunction.html" title="jQuery.isFunction()  方法">jQuery.isFuncti...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-jstl.html" rel="prev"> JSP 标准标签库（JSTL）</a> </div>
			<div class="next-design-link"></div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>Eclipse JSP/Servlet 环境搭建</h1>
<p>本文假定你已安装了 JDK 环境，如未安装，可参阅 <a target="_blank" title="Java 开发环境配置" href="/java/java-environment-setup.html"> Java 开发环境配置 </a>。</p>
<p>我们可以使用 Eclipse 来搭建 JSP 开发环境，首先我们分别下载一下软件包：</p>
<ul><li>
<strong>Eclipse J2EE：</strong><a href="http://www.eclipse.org/downloads/" target="_blank">http://www.eclipse.org/downloads/</a>
</li><li>
<strong>Tomcat：</strong><a href="http://tomcat.apache.org/download-70.cgi" target="_blank">http://tomcat.apache.org/download-70.cgi</a>
</li></ul>

<hr>
<h2>Tomcat 下载安装</h2>
<p>你可以根据你的系统下载对应的包(以下以Window系统为例)：</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/232104286524890.png" />
</p>
<p>
下载之后，将压缩包解压到D盘（你可以自己选择）：
</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/232104410586765.png" />
</p>
<p>注意目录名不能有中文和空格。目录介绍如下：<p>
<ul>
<li>bin：二进制执行文件。里面最常用的文件是<strong>startup.bat</strong>，如果是 Linux 或 Mac 系统启动文件为 <strong>startup.sh</strong>。</li>
<li>conf:配置目录。里面最核心的文件是<strong>server.xml</strong>。可以在里面改端口号等。默认端口号是8080，也就是说，此端口号不能被其他应用程序占用。</li>
<li>lib：库文件。tomcat运行时需要的jar包所在的目录</li>
<li>logs：日志</li>
<li>temp：临时产生的文件，即缓存</li>
<li>webapps：web的应用程序。<strong>web应用放置到此目录下浏览器可以直接访问</strong></li>
<li>work：编译以后的class文件。</li>
</ul>
<p>接着我们可以双击 startup.bat 启动 Tomcat，弹出如下界面：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2016/01/232105392158264.png"/></p>
<p>这个时候，本地的服务器就已经搭建起来了。如果想关闭服务器，可以直接关闭上面的窗口，或者在里面输入Ctrl+C禁止服务。</p>
<p>接着我们在浏览器中输入 <b>http://localhost:8080/</b>，如果弹出如下界面，表示tomcat安装成功并且启动起来了：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2016/01/tomcat-index.jpg" /></p>
<p>我们现在在浏览器上测试一下它吧：
</p><p>
首先在D:\apache-tomcat-8.0.14\webapps\ROOT目录中新建一个jsp文件：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2016/01/232106198557249.jpg"></p>
<p>test.jsp 文件代码如下：</p>
<pre>
&lt;%@ page contentType="text/html;charset=UTF-8" %&gt;
&lt;%
out.print("菜鸟教程 : http://www.runoob.com");
%&gt; 
</pre>
<p>接着在浏览器中访问地址 <b>http://localhost:8080/test.jsp</b>, 输出结果如下：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2016/01/test-jsp.jpg"></p>
<hr>
<h2>将 Tomcat 和 Eclipse 相关联</h2>
<p>Eclipse J2EE下载后，解压即可使用，我们打开Java EE ，选择菜单栏Windows-->preferences（Mac 系统为 Eclipse-->偏好设置），弹出如下界面：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2016/01/232111301681549.png"/></p>
<p>上图中，点击"add"的添加按钮，弹出如下界面：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2016/01/232111442933866.png" /></p>
<p>在选项中，我们选择对应的 Tomcat 版本，接着点击 "Next"，选择 Tomcat 的安装目录，并选择我们安装的 Java 环境：</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/232112245587963.png" /></p>
<p>点击 "Finish"，完成配置。</p>
<h3>创建实例</h3>
<p>选择 "File-->New-->Dynamic Web Project"，创建 TomcatTest 项目：</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/232112541213100.png" /></p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/302044303245040.png" /></p>
<p>点开上图中的红框部分，弹出如下界面：</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/8998BEC1-D622-4BD4-A9E6-8B18D2A5F29C.jpg" /></p>
<p>注意如果已默认选择了我们之前安装的 Tomcat 和 JDK 则可跳过此步。</p>
<p>然后，单击finish, 继续：</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/232113121219000.png" /></p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/232113256216676.png" /></p>
<p>工程文件结构：</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/232113367466511.png" /></p>
<p>上图中各个目录解析：</p>
<ul><li>deployment descriptor：部署的描述。</li><li>
Web App Libraries：自己加的包可以放在里面。</li><li>
build：放入编译之后的文件。</li><li>
WebContent:放进写入的页面。</li></ul>
<p>
在WebContent文件夹下新建一个test.jsp文件。在下图中可以看到它的默认代码：
</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>接着我们修改下test.jsp文件代码如下所示：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;菜鸟教程&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
	out.println("Hello World!");
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>程序运行之前，我们先修改一下浏览器选项:</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/testjsp1.png"></p>

<p>接着我们运行该项目:</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/runas.png"></p><p>
运行时，弹出如下错误：(如果没有此错误，请忽略)</p><p>

<img src="http://www.runoob.com/wp-content/uploads/2016/01/232120047932694.png"></p><p>

原因是，我们之前点击了Tomcat安装包中的​startup.bat，这样一来就手动打开了Tomcat服务器，这明显是多余的，因为程序运行时，eclipse会自动开启Tomcat服务器。所以我们先手动关掉tomcat软件，再次运行程序，就行了。控制台信息如下：</p><p>

<img src="http://www.runoob.com/wp-content/uploads/2016/01/232120199803353.png"></p>
<p>浏览器访问 <b>http://localhost:8080/TomcatTest/test.jsp</b>, 即可输出正常结果：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2016/01/A72F19CD-4FEA-4AE3-8D91-43B34623EC37.jpg">
</p>
<hr>
<h2>Servlet 实例创建</h2>
<p>我们也可以使用以上环境创建 Servlet 文件，选择 "File-->New-->Servlet":</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/sns.png"></p>
<p>位于 TomcatTest项目的 /TomcatTest/src 目录下创建 "HelloServlet" 类，包为 "com.runoob.test":</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/22D8CED0-F2DD-4554-BFBD-2B19D1685FB9.jpg"/></p>
<p>HelloServlet.java  代码如下所示：</p>
<pre>
package com.runoob.test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloServlet
 */
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 使用 GBK 设置中文正常显示
		response.setCharacterEncoding("GBK");
		response.getWriter().write("菜鸟教程：http://www.runoob.com");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
</pre>
<p>创建 /TomcatTest/WebContent/WEB-INF/web.xml 文件（如果没有），代码如下所示：</p>
<pre>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app version="2.5"   
    xmlns="http://java.sun.com/xml/ns/javaee"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;  
  &lt;servlet&gt;  
     &lt;!-- 类名 --&gt;  
    &lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;  
    &lt;!-- 所在的包 --&gt;  
    &lt;servlet-class&gt;com.runoob.test.HelloServlet&lt;/servlet-class&gt;  
  &lt;/servlet&gt;  
  &lt;servlet-mapping&gt;  
    &lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;  
    &lt;!-- 访问的网址 --&gt;  
    &lt;url-pattern&gt;/TomcatTest/HelloServlet&lt;/url-pattern&gt;  
    &lt;/servlet-mapping&gt;  
&lt;/web-app&gt;  
</pre>
<p>接着重启 Tomcat，浏览器访问 <b>http://localhost:8080/TomcatTest/HelloServlet</b>：</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2016/01/3E00DBEA-85CA-4F66-A7E9-24C43BC9C756.jpg"></p>
<p>
参考文章：http://www.cnblogs.com/smyhvae/p/4046862.html</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-jstl.html" rel="prev"> JSP 标准标签库（JSTL）</a> </div>
			<div class="next-design-link"></div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-setup.html" rel="prev"> JSP 开发环境搭建</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-life-cycle.html" rel="next"> JSP 生命周期</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">结构</span>
</h1>

<p>
网络服务器需要一个JSP引擎，也就是一个容器来处理JSP页面。容器负责截获对JSP页面的请求。本教程使用内嵌JSP容器的Apache来支持JSP开发。
</p><p>
JSP容器与Web服务器协同合作，为JSP的正常运行提供必要的运行环境和其他服务，并且能够正确识别专属于JSP网页的特殊元素。
</p>
<p>下图显示了JSP容器和JSP文件在Web应用中所处的位置。
</p>

<p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp-arch.jpg" />

<hr>
<h2>
JSP处理
</h2>
<p>
以下步骤表明了Web服务器是如何使用JSP来创建网页的：
</p>
<ul>
<li>
就像其他普通的网页一样，您的浏览器发送一个HTTP请求给服务器。</li><li>
Web服务器识别出这是一个对JSP网页的请求，并且将该请求传递给JSP引擎。通过使用URL或者.jsp文件来完成。</li><li>
JSP引擎从磁盘中载入JSP文件，然后将它们转化为servlet。这种转化只是简单地将所有模板文本改用println()语句，并且将所有的JSP元素转化成Java代码。</li><li>
JSP引擎将servlet编译成可执行类，并且将原始请求传递给servlet引擎。</li><li>
Web服务器的某组件将会调用servlet引擎，然后载入并执行servlet类。在执行过程中，servlet产生HTML格式的输出并将其内嵌于HTTP response中上交给Web服务器。</li><li>
Web服务器以静态HTML网页的形式将HTTP response返回到您的浏览器中。</li><li>
最终，Web浏览器处理HTTP response中动态产生的HTML网页，就好像在处理静态网页一样。</li></ul>
<p>
以上提及到的步骤可以用下图来表示：
</p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp-processing.jpg" />

<p>
一般情况下，JSP引擎会检查JSP文件对应的servlet是否已经存在，并且检查JSP文件的修改日期是否早于servlet。如果JSP文件的修改日期早于对应的servlet，那么容器就可以确定JSP文件没有被修改过并且servlet有效。这使得整个流程与其他脚本语言（比如PHP）相比要高效快捷一些。
</p><p>
总的来说，JSP网页就是用另一种方式来编写servlet而不用成为Java编程高手。除了解释阶段外，JSP网页几乎可以被当成一个普通的servlet来对待。
</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-setup.html" rel="prev"> JSP 开发环境搭建</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-life-cycle.html" rel="next"> JSP 生命周期</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-architecture.html" rel="prev"> JSP 结构</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-syntax.html" rel="next"> JSP 语法</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">生命周期</span>
</h1>
<p>
理解JSP底层功能的关键就是去理解它们所遵守的生命周期。
</p><p>
JSP生命周期就是从创建到销毁的整个过程，类似于servlet生命周期，区别在于JSP生命周期还包括将JSP文件编译成servlet。
</p><p>
以下是JSP生命周期中所走过的几个阶段：
</p>
<ul>
<li><p><strong>
编译阶段：</strong><p>servlet容器编译servlet源文件，生成servlet类</p></li><li><strong>
初始化阶段：</strong><p>加载与JSP对应的servlet类，创建其实例，并调用它的初始化方法</p></li><li><strong>
执行阶段：</strong><p>调用与JSP对应的servlet实例的服务方法</p></li><li><strong>
销毁阶段：</strong><p>调用与JSP对应的servlet实例的销毁方法，然后销毁servlet实例</p></li></ul>
<p>
很明显，JSP生命周期的四个主要阶段和servlet生命周期非常相似，下面给出图示：
</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp_life_cycle.jpg" />
</p>

<hr><h2>
JSP编译
</h2>
<p>
当浏览器请求JSP页面时，JSP引擎会首先去检查是否需要编译这个文件。如果这个文件没有被编译过，或者在上次编译后被更改过，则编译这个JSP文件。
</p>
<p>
编译的过程包括三个步骤：</p>
<ul>
<li>
解析JSP文件。</li><li>
将JSP文件转为servlet。</li><li>
编译servlet。</li></ul>
<hr><h2>
JSP初始化</h2>

<p>容器载入JSP文件后，它会在为请求提供任何服务前调用jspInit()方法。如果您需要执行自定义的JSP初始化任务，复写jspInit()方法就行了，就像下面这样：
</p>
<pre>
public void jspInit(){
  // 初始化代码
}
</pre>
<p>
一般来讲程序只初始化一次，servlet也是如此。通常情况下您可以在jspInit()方法中初始化数据库连接、打开文件和创建查询表。
</p>
<hr>
<h2>
JSP执行
</h2>
<p>
这一阶段描述了JSP生命周期中一切与请求相关的交互行为，直到被销毁。
</p>
<p>
当JSP网页完成初始化后，JSP引擎将会调用_jspService()方法。
</p><p>
_jspService()方法需要一个HttpServletRequest对象和一个HttpServletResponse对象作为它的参数，就像下面这样：
</p>
<pre>
void _jspService(HttpServletRequest request,
                 HttpServletResponse response)
{
   // 服务端处理代码
}
</pre><p>
_jspService()方法在每个request中被调用一次并且负责产生与之相对应的response，并且它还负责产生所有7个HTTP方法的回应，比如GET、POST、DELETE等等。
</p><hr>
<h2>
JSP清理
</h2><p>
JSP生命周期的销毁阶段描述了当一个JSP网页从容器中被移除时所发生的一切。
</p><p>
jspDestroy()方法在JSP中等价于servlet中的销毁方法。当您需要执行任何清理工作时复写jspDestroy()方法，比如释放数据库连接或者关闭文件夹等等。
</p><p>
jspDestroy()方法的格式如下：
</p>
<pre>
public void jspDestroy()
{
   // 清理代码
}
</pre>
<h3>实例</h3>
<p>JSP生命周期代码实例如下所示：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;life.jsp&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;%! 
  private int initVar=0;
  private int serviceVar=0;
  private int destroyVar=0;
%&gt;
  
&lt;%!
  public void jspInit(){
    initVar++;
    System.out.println("jspInit(): JSP被初始化了"+initVar+"次");
  }
  public void jspDestroy(){
    destroyVar++;
    System.out.println("jspDestroy(): JSP被销毁了"+destroyVar+"次");
  }
%&gt;

&lt;%
  serviceVar++;
  System.out.println("_jspService(): JSP共响应了"+serviceVar+"次请求");

  String content1="初始化次数 : "+initVar;
  String content2="响应客户请求次数 : "+serviceVar;
  String content3="销毁次数 : "+destroyVar;
%&gt;
&lt;h1&gt;菜鸟教程 JSP 测试实例&lt;/h1&gt;
&lt;p&gt;&lt;%=content1 %&gt;&lt;/p&gt;
&lt;p&gt;&lt;%=content2 %&gt;&lt;/p&gt;
&lt;p&gt;&lt;%=content3 %&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>浏览器打开该页面，输出结果为：</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/E80496E2-35DF-439F-8A43-6376D92DFA45.jpg" width="70%"></p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-architecture.html" rel="prev"> JSP 结构</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-syntax.html" rel="next"> JSP 语法</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-life-cycle.html" rel="prev"> JSP 生命周期</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-directives.html" rel="next"> JSP 指令</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">语法</span>
</h1>

<p>本小节将会简单地介绍一下JSP开发中的基础语法。
</p>

<hr>
<h2>脚本程序</h2>
<p>脚本程序可以包含任意量的Java语句、变量、方法或表达式，只要它们在脚本语言中是有效的。</p>
<p>
脚本程序的语法格式：</p>
<pre>
&lt;% 代码片段 %&gt;
</pre>
<p>
或者，您也可以编写与其等价的XML语句，就像下面这样：</p>
<pre>
&lt;jsp:scriptlet&gt;
   代码片段
&lt;/jsp:scriptlet&gt;
</pre>
<p>
任何文本、HTML标签、JSP元素必须写在脚本程序的外面。</p>
<p>
下面给出一个示例，同时也是本教程的第一个JSP示例：</p>
<pre>
&lt;html&gt;
&lt;head&gt;&lt;title&gt;Hello World&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
Hello World!&lt;br/&gt;
&lt;%
out.println("Your IP address is " + request.getRemoteAddr());
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p><b>注意：</b>请确保Apache Tomcat已经安装在C:\apache-tomcat-7.0.2目录下并且运行环境已经正确设置。</p>
<p>
将以上代码保存在hello.jsp中，然后将它放置在 C:\apache-tomcat-7.0.2\webapps\ROOT目录下，打开浏览器并在地址栏中输入http://localhost:8080/hello.jsp。运行后得到以下结果：</p>
<p>
<img  src="http://www.runoob.com/wp-content/uploads/2014/01/jsp_hello_world.jpg" >

</p>

<h3>中文编码问题</h3>
<p>如果我们要在页面正常显示中文，我们需要在 JSP 文件头部添加以下代码：<>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
</pre>
<p>解析来我们将以上程序修改为：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
Hello World!&lt;br/&gt;
&lt;%
out.println("你的 IP 地址 " + request.getRemoteAddr());
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>这样中文就可以正常显示了。</p>

<hr>
<h2>
	JSP声明</h2>
<p>一个声明语句可以声明一个或多个变量、方法，供后面的Java代码使用。在JSP文件中，您必须先声明这些变量和方法然后才能使用它们。</p>
<p>
JSP声明的语法格式：</p>
<pre>
&lt;%! declaration; [ declaration; ]+ ... %&gt;
</pre>
<p>
或者，您也可以编写与其等价的XML语句，就像下面这样：</p>
<pre>
&lt;jsp:declaration&gt;
   代码片段
&lt;/jsp:declaration&gt;
</pre>
<p>
程序示例：</p>
<pre>
&lt;%! int i = 0; %&gt; 
&lt;%! int a, b, c; %&gt; 
&lt;%! Circle a = new Circle(2.0); %&gt; 
</pre>
<hr>
<h2>
	JSP表达式</h2><p>
一个JSP表达式中包含的脚本语言表达式，先被转化成String，然后插入到表达式出现的地方。</p><p>
由于表达式的值会被转化成String，所以您可以在一个文本行中使用表达式而不用去管它是否是HTML标签。</p><p>

表达式元素中可以包含任何符合Java语言规范的表达式，但是不能使用分号来结束表达式。</p><p>
JSP表达式的语法格式：</p>
<pre>
&lt;%= 表达式 %&gt;
</pre>
<p>
同样，您也可以编写与之等价的XML语句：</p>
<pre>
&lt;jsp:expression&gt;
   表达式
&lt;/jsp:expression&gt;
</pre><p>
程序示例：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;
   今天的日期是: &lt;%= (new java.util.Date()).toLocaleString()%&gt;
&lt;/p&gt;
&lt;/body&gt; 
&lt;/html&gt; 
</pre>
<p>运行后得到以下结果：</p>
<pre>
今天的日期是: 2016-6-25 13:40:07
</pre><hr>
<h2>
	JSP注释</h2>
<p>JSP注释主要有两个作用：为代码作注释以及将某段代码注释掉。</p>
<p>
JSP注释的语法格式：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%-- 该部分注释在网页中不会被显示--%&gt; 
&lt;p&gt;
   今天的日期是: &lt;%= (new java.util.Date()).toLocaleString()%&gt;
&lt;/p&gt;
&lt;/body&gt; 
&lt;/html&gt; 
</pre>
<p>
运行后得到以下结果：</p>
<pre>
今天的日期是: 2016-6-25 13:41:26
</pre>
<p>
不同情况下使用注释的语法规则：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>语法</strong></th>
			<th>
				描述</th>
		</tr>
		<tr>
			<td>
				&lt;%-- 注释 --%&gt;</td>
			<td>
				JSP注释，注释内容不会被发送至浏览器甚至不会被编译</td>
		</tr>
		<tr>
			<td>
				&lt;!-- 注释 --&gt;</td>
			<td>
				HTML注释，通过浏览器查看网页源代码时可以看见注释内容</td>
		</tr>
		<tr>
			<td>
				&lt;\%</td>
			<td>
				代表静态 &lt;%常量</td>
		</tr>
		<tr>
			<td>
				%\&gt;</td>
			<td>
				代表静态 %&gt; 常量</td>
		</tr>
		<tr>
			<td>
				\'</td>
			<td>
				在属性中使用的单引号</td>
		</tr>
		<tr>
			<td>
				\"</td>
			<td>
				在属性中使用的双引号</td>
		</tr>
	</tbody>
</table>
<hr>
<h2>
	JSP指令</h2>
	<p>
JSP指令用来设置与整个JSP页面相关的属性。	</p>
<p>
JSP指令语法格式：</p>
<pre>
&lt;%@ directive attribute="value" %&gt;
</pre>
<p>
这里有三种指令标签：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>指令</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				&lt;%@ page ... %&gt;</td>
			<td>
				定义页面的依赖属性，比如脚本语言、error页面、缓存需求等等</td>
		</tr>
		<tr>
			<td>
				&lt;%@ include ... %&gt;</td>
			<td>
				包含其他文件</td>
		</tr>
		<tr>
			<td>
				&lt;%@ taglib ... %&gt;</td>
			<td>
				引入标签库的定义，可以是自定义标签</td>
		</tr>
	</tbody>
</table>
<hr>
<h2>
	JSP行为</h2>
	<p>
JSP行为标签使用XML语法结构来控制servlet引擎。它能够动态插入一个文件，重用JavaBean组件，引导用户去另一个页面，为Java插件产生相关的HTML等等。</p>
<p>
行为标签只有一种语法格式，它严格遵守XML标准：</p>
<pre>
&lt;jsp:action_name attribute="value" /&gt;
</pre><p>
行为标签基本上是一些预先就定义好的函数，下表罗列出了一些可用的JSP行为标签：：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>语法</strong></th>
			<th>
				<b>描述</b></th>
		</tr>
		<tr>
			<td>
				jsp:include</td>
			<td>
				用于在当前页面中包含静态或动态资源</td>
		</tr>
		<tr>
			<td>
				jsp:useBean</td>
			<td>
				寻找和初始化一个JavaBean组件</td>
		</tr>
		<tr>
			<td>
				jsp:setProperty</td>
			<td>
				设置 JavaBean组件的值</td>
		</tr>
		<tr>
			<td>
				jsp:getProperty</td>
			<td>
				将&nbsp;JavaBean组件的值插入到 output中</td>
		</tr>
		<tr>
			<td>
				jsp:forward</td>
			<td>
				从一个JSP文件向另一个文件传递一个包含用户请求的request对象</td>
		</tr>
		<tr>
			<td>
				jsp:plugin</td>
			<td>
				用于在生成的HTML页面中包含Applet和JavaBean对象</td>
		</tr>
		<tr>
			<td>
				jsp:element</td>
			<td>
				动态创建一个XML元素</td>
		</tr>
		<tr>
			<td>
				jsp:attribute</td>
			<td>
				定义动态创建的XML元素的属性</td>
		</tr>
		<tr>
			<td>
				jsp:body</td>
			<td>
				定义动态创建的XML元素的主体</td>
		</tr>
		<tr>
			<td>
				jsp:text</td>
			<td>
				用于封装模板数据</td>
		</tr>
	</tbody>
</table>
<hr>
<h2>
	JSP隐含对象</h2><p>
JSP支持九个自动定义的变量，江湖人称隐含对象。这九个隐含对象的简介见下表：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>对象</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				request</td>
			<td>
				<strong>HttpServletRequest</strong>类的实例</td>
		</tr>
		<tr>
			<td>
				response</td>
			<td>
				<strong>HttpServletResponse</strong>类的实例</td>
		</tr>
		<tr>
			<td>
				out</td>
			<td>
				<strong>PrintWriter</strong>类的实例，用于把结果输出至网页上</td>
		</tr>
		<tr>
			<td>
				session</td>
			<td>
				<strong>HttpSession</strong>类的实例</td>
		</tr>
		<tr>
			<td>
				application</td>
			<td>
				<strong>ServletContext</strong>类的实例，与应用上下文有关</td>
		</tr>
		<tr>
			<td>
				config</td>
			<td>
				<strong>ServletConfig</strong>类的实例</td>
		</tr>
		<tr>
			<td>
				pageContext</td>
			<td>
				<strong>PageContext</strong>类的实例，提供对JSP页面所有对象以及命名空间的访问</td>
		</tr>
		<tr>
			<td>
				page</td>
			<td>
				类似于Java类中的this关键字</td>
		</tr>
		<tr>
			<td>
				Exception</td>
			<td>
				<strong>Exception</strong>类的对象，代表发生错误的JSP页面中对应的异常对象</td>
		</tr>
	</tbody>
</table>
<hr>
<h2>
	控制流语句</h2><p>
JSP提供对Java语言的全面支持。您可以在JSP程序中使用Java API甚至建立Java代码块，包括判断语句和循环语句等等。</p>
<h2>
	判断语句</h2><p>
If…else块，请看下面这个例子：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%! int day = 3; %&gt; 
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;IF...ELSE 实例&lt;/h3&gt;
&lt;% if (day == 1 | day == 7) { %&gt;
      &lt;p&gt;今天是周末&lt;/p&gt;
&lt;% } else { %&gt;
      &lt;p&gt;今天不是周末&lt;/p&gt;
&lt;% } %&gt;
&lt;/body&gt; 
&lt;/html&gt; 
</pre>
<p>
运行后得到以下结果：
</p>
<pre>
IF...ELSE 实例
今天不是周末
</pre>
<p>
现在来看看switch…case块，与if…else块有很大的不同，它使用out.println()，并且整个都装在脚本程序的标签中，就像下面这样：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%! int day = 3; %&gt; 
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;SWITCH...CASE 实例&lt;/h3&gt;
&lt;% 
switch(day) {
case 0:
   out.println("星期天");
   break;
case 1:
   out.println("星期一");
   break;
case 2:
   out.println("星期二");
   break;
case 3:
   out.println("星期三");
   break;
case 4:
   out.println("星期四");
   break;
case 5:
   out.println("星期五");
   break;
default:
   out.println("星期六");
}
%&gt;
&lt;/body&gt; 
&lt;/html&gt; 
</pre>
<p>
浏览器访问，运行后得出以下结果：</p>
<pre>
SWITCH...CASE 实例

星期三
</pre>
	<hr><h2>循环语句</h2>
	<p>
在JSP程序中可以使用Java的三个基本循环类型：for，while，和 do…while。</p><p>
让我们来看看for循环的例子，以下输出的不同字体大小的"菜鸟教程"：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%! int fontSize; %&gt; 
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;For 循环实例&lt;/h3&gt;
&lt;%for ( fontSize = 1; fontSize &lt;= 3; fontSize++){ %&gt;
   &lt;font color="green" size="&lt;%= fontSize %&gt;"&gt;
    菜鸟教程
   &lt;/font&gt;&lt;br /&gt;
&lt;%}%&gt;
&lt;/body&gt; 
&lt;/html&gt; 
</pre>
<p>
运行后得到以下结果：</p>
<p><img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/7B4B85CF-FE4B-43CB-AAFF-F8594AD4342C.jpg"></p>
<p>
将上例改用while循环来写：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%! int fontSize; %&gt; 
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;While 循环实例&lt;/h3&gt;
&lt;%while ( fontSize &lt;= 3){ %&gt;
   &lt;font color="green" size="&lt;%= fontSize %&gt;"&gt;
    菜鸟教程
   &lt;/font&gt;&lt;br /&gt;
&lt;%fontSize++;%&gt;
&lt;%}%&gt;
&lt;/body&gt; 
&lt;/html&gt; 
</pre>
浏览器访问，输出结果为（fontSize 初始化为0，所以多输出了一行）：<br>
<p><img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/4F744CC9-E484-45BA-AF18-27AFCF4AD45C.jpg"></p>
	JSP运算符</h2><p>
JSP支持所有Java逻辑和算术运算符。</p><p>
下表罗列出了JSP常见运算符，优先级从高到底：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>类别</strong></th>
			<th>
				<strong>操作符</strong></th>
			<th>
				<strong>结合性</strong></th>
		</tr>
		<tr>
			<td>
				后缀</td>
			<td>
				() [] . (点运算符)</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				一元</td>
			<td>
				++ - - ! ~</td>
			<td>
				右到左</td>
		</tr>
		<tr>
			<td>
				可乘性</td>
			<td>
				* / %&nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				可加性</td>
			<td>
				+ -&nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				移位</td>
			<td>
				&gt;&gt; &gt;&gt;&gt; &lt;&lt; &nbsp;</td>
			<td>
				左到右&nbsp;</td>
		</tr>
		<tr>
			<td>
				关系</td>
			<td>
				&gt; &gt;= &lt; &lt;= &nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				相等/不等</td>
			<td>
				== !=&nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				位与</td>
			<td>
				&amp;&nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				位异或</td>
			<td>
				^&nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				位或</td>
			<td>
				|&nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				逻辑与</td>
			<td>
				&amp;&amp;&nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				逻辑或</td>
			<td>
				||&nbsp;</td>
			<td>
				左到右</td>
		</tr>
		<tr>
			<td>
				条件判断</td>
			<td>
				?:&nbsp;</td>
			<td>
				右到左</td>
		</tr>
		<tr>
			<td>
				赋值</td>
			<td>
				= += -= *= /= %= &gt;&gt;= &lt;&lt;= &amp;= ^= |=&nbsp;</td>
			<td>
				右到左</td>
		</tr>
		<tr>
			<td>
				逗号&nbsp;</td>
			<td>
				,&nbsp;</td>
			<td>
				左到右&nbsp;</td>
		</tr>
	</tbody>
</table>
<hr>
<h2>
	JSP 字面量</h2><p>
JSP语言定义了以下几个字面量：</p>
<ul>
	<li>
		布尔值(boolean)：true 和 false;</li>
	<li>
		整型(int)：与 Java 中的一样;</li>
	<li>
		浮点型(float)：与 Java 中的一样;</li>
	<li>
		字符串(string)：以单引号或双引号开始和结束;</li>
	<li>
		Null：null。</li>
</ul>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-life-cycle.html" rel="prev"> JSP 生命周期</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-directives.html" rel="next"> JSP 指令</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-syntax.html" rel="prev"> JSP 语法</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-actions.html" rel="next"> JSP 动作元素</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">指令</span></h1>
 <p>JSP指令用来设置整个JSP页面相关的属性，如网页的编码方式和脚本语言。</p>
 <p>
语法格式如下：</p>
<pre>
&lt;%@ directive attribute="value" %&gt;
</pre> <p>
指令可以有很多个属性，它们以键值对的形式存在，并用逗号隔开。</p>
<p>
JSP中的三种指令标签：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>指令</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				&lt;%@ page ... %&gt;</td>
			<td>
				定义网页依赖属性，比如脚本语言、error页面、缓存需求等等</td>
		</tr>
		<tr>
			<td>
				&lt;%@ include ... %&gt;</td>
			<td>
				包含其他文件</td>
		</tr>
		<tr>
			<td>
				&lt;%@ taglib ... %&gt;</td>
			<td>
				引入标签库的定义</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	Page指令</h2>
	<p>
Page指令为容器提供当前页面的使用说明。一个JSP页面可以包含多个page指令。</p>
	<p>
Page指令的语法格式：</p>
<pre>
&lt;%@ page attribute="value" %&gt;
</pre>
<p>
等价的XML格式：</p>
<pre>
&lt;jsp:directive.page attribute="value" /&gt;
</pre><hr>
<h2>
	属性</h2><p>
下表列出与Page指令相关的属性：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>属性</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				buffer</td>
			<td>
				指定out对象使用缓冲区的大小</td>
		</tr>
		<tr>
			<td>
				autoFlush</td>
			<td>
				控制out对象的&nbsp;缓存区</td>
		</tr>
		<tr>
			<td>
				contentType</td>
			<td>
				指定当前JSP页面的MIME类型和字符编码</td>
		</tr>
		<tr>
			<td>
				errorPage</td>
			<td>
				指定当JSP页面发生异常时需要转向的错误处理页面</td>
		</tr>
		<tr>
			<td>
				isErrorPage</td>
			<td>
				指定当前页面是否可以作为另一个JSP页面的错误处理页面</td>
		</tr>
		<tr>
			<td>
				extends</td>
			<td>
				指定servlet从哪一个类继承</td>
		</tr>
		<tr>
			<td>
				import</td>
			<td>
				导入要使用的Java类</td>
		</tr>
		<tr>
			<td>
				info</td>
			<td>
				定义JSP页面的描述信息</td>
		</tr>
		<tr>
			<td>
				isThreadSafe</td>
			<td>
				指定对JSP页面的访问是否为线程安全</td>
		</tr>
		<tr>
			<td>
				language</td>
			<td>
				定义JSP页面所用的脚本语言，默认是Java</td>
		</tr>
		<tr>
			<td>
				session</td>
			<td>
				指定JSP页面是否使用session</td>
		</tr>
		<tr>
			<td>
				isELIgnored</td>
			<td>
				指定是否执行EL表达式</td>
		</tr>
		<tr>
			<td>
				isScriptingEnabled</td>
			<td>
				确定脚本元素能否被使用</td>
		</tr>
	</tbody>
</table>
<hr>

<h2>
	Include指令</h2>
	<p>
JSP可以通过include指令来包含其他文件。被包含的文件可以是JSP文件、HTML文件或文本文件。包含的文件就好像是该JSP文件的一部分，会被同时编译执行。</p>
<p>
Include指令的语法格式如下：</p>
<pre>
&lt;%@ include file="文件相对 url 地址" %&gt;
</pre>
<p>
<strong>include</strong> 指令中的文件名实际上是一个相对的 URL 地址。</p>
<p>如果您没有给文件关联一个路径，JSP编译器默认在当前路径下寻找。</p>
<p>
等价的XML语法：</p>
<pre>
&lt;jsp:directive.include file="文件相对 url 地址" /&gt;
</pre>
<hr>
<h2>
	Taglib指令</h2>
<p>
JSP API允许用户自定义标签，一个自定义标签库就是自定义标签的集合。</p>
<p>
Taglib指令引入一个自定义标签集合的定义，包括库路径、自定义标签。</p>
<p>
Taglib指令的语法：</p>
<pre>
&lt;%@ taglib uri="uri" prefix="prefixOfTag" %&gt;
</pre>
<p>
uri属性确定标签库的位置，prefix属性指定标签库的前缀。</p>
<p>
等价的XML语法：</p>
<pre>
&lt;jsp:directive.taglib uri="uri" prefix="prefixOfTag" /&gt;
</pre>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-syntax.html" rel="prev"> JSP 语法</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-actions.html" rel="next"> JSP 动作元素</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-directives.html" rel="prev"> JSP 指令</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-implicit-objects.html" rel="next"> JSP 隐式对象</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">动作元素</span></h1>
<p>
与JSP指令元素不同的是，JSP动作元素在请求处理阶段起作用。JSP动作元素是用XML语法写成的。</p>
<p>利用JSP动作可以动态地插入文件、重用JavaBean组件、把用户重定向到另外的页面、为Java插件生成HTML代码。 </p>
<p>动作元素只有一种语法，它符合XML标准：</p>
<pre>
&lt;jsp:action_name attribute="value" /&gt;
</pre>
<p>动作元素基本上都是预定义的函数，JSP规范定义了一系列的标准动作，它用JSP作为前缀，可用的标准动作元素如下：</p>
<table class="reference">
<tbody><tr><th style="width:30%">语法 </th><th>描述 </th></tr>
<tr><td>jsp:include</td><td>在页面被请求的时候引入一个文件。 </td></tr>
<tr><td>jsp:useBean</td><td>寻找或者实例化一个JavaBean。 </td></tr>
<tr><td>jsp:setProperty</td><td>设置JavaBean的属性。 </td></tr>
<tr><td>jsp:getProperty</td><td>输出某个JavaBean的属性。 </td></tr>
<tr><td>jsp:forward</td><td>把请求转到一个新的页面。 </td></tr>
<tr><td>jsp:plugin</td><td>根据浏览器类型为Java插件生成OBJECT或EMBED标记。 </td></tr>
<tr><td>jsp:element</td><td>定义动态XML元素</td></tr>
<tr><td>jsp:attribute</td><td>设置动态定义的XML元素属性。</td></tr>
<tr><td>jsp:body</td><td>设置动态定义的XML元素内容。</td></tr>
<tr><td>jsp:text</td><td>在JSP页面和文档中使用写入文本的模板</td></tr>
</tbody></table>
<hr>
<h2>常见的属性</h2>
<p>
所有的动作要素都有两个属性：id属性和scope属性。
</p>
<ul><li><strong>
id属性：</strong><p>id属性是动作元素的唯一标识，可以在JSP页面中引用。动作元素创建的id值可以通过PageContext来调用。
<p></li><li><strong>
scope属性：</strong><p></strong>该属性用于识别动作元素的生命周期。 id属性和scope属性有直接关系，scope属性定义了相关联id对象的寿命。 scope属性有四个可能的值： (a) page, (b)request, (c)session, 和 (d) application。

<p></li></ul>
<hr>
<h2> &lt;jsp:include&gt;动作元素  </h2>
<p>  &lt;jsp:include&gt;动作元素用来包含静态和动态的文件。该动作把指定文件插入正在生成的页面。语法格式如下：</p>
<pre>
&lt;jsp:include page="相对 URL 地址" flush="true" /&gt;
</pre>
<p>　前面已经介绍过include指令，它是在JSP文件被转换成Servlet的时候引入文件，而这里的jsp:include动作不同，插入文件的时间是在页面被请求的时候。
</p>
<p>以下是include动作相关的属性列表。</p>
<table class="reference">
<tbody><tr><th style="width:30%">属性 </th><th>描述 </th></tr>
<tr><td>page</td><td>包含在页面中的相对URL地址。</td></tr>
<tr><td>flush</td><td>布尔属性，定义在包含资源前是否刷新缓存区。</td></tr>
</tbody></table>
<h3>实例</h3>
<p>以下我们定义了两个文件 <strong>date.jsp</strong> 和 <strong>main.jsp</strong>，代码如下所示：</p>
<p>date.jsp文件代码：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;p&gt;
   今天的日期是: &lt;%= (new java.util.Date()).toLocaleString()%&gt;
&lt;/p&gt;
</pre>
<p>main.jsp文件代码：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h2&gt;include 动作实例&lt;/h2&gt;
&lt;jsp:include page="date.jsp" flush="true" /&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>现在将以上两个文件放在服务器的根目录下，访问main.jsp文件。显示结果如下：</p>
<pre>
include 动作实例

今天的日期是: 2016-6-25 14:08:17
</pre>
<hr>
<h2>&lt;jsp:useBean&gt;动作元素</h2>
<p><strong>jsp:useBean</strong> 动作用来加载一个将在JSP页面中使用的JavaBean。</p>
<p>这个功能非常有用，因为它使得我们可以发挥 Java 组件复用的优势。</p>
<p>jsp:useBean动作最简单的语法为： 
</p>
<pre>
&lt;jsp:useBean id="name" class="package.class" /&gt;
</pre>
<p>
在类载入后，我们既可以通过 jsp:setProperty 和 jsp:getProperty 动作来修改和检索bean的属性。 
</p>
<p>以下是useBean动作相关的属性列表。</p>
<table class="reference">
<tbody><tr><th style="width:30%">属性 </th><th>描述 </th></tr>
<tr><td>class</td><td>指定Bean的完整包名。</td></tr>
<tr><td>type</td><td>指定将引用该对象变量的类型。</td></tr>
<tr><td>beanName</td><td>通过 java.beans.Beans 的 instantiate() 方法指定Bean的名字。</td></tr>
</tbody></table>
<p>在给出具体实例前，让我们先来看下 jsp:setProperty 和 jsp:getProperty 动作元素：</p>
<hr>
<h2> &lt;jsp:setProperty&gt;动作元素</h2>
<p>jsp:setProperty用来设置已经实例化的Bean对象的属性，有两种用法。首先，你可以在jsp:useBean元素的外面（后面）使用jsp:setProperty，如下所示： 
</p>
<pre>
&lt;jsp:useBean id="myName" ... /&gt;
...
&lt;jsp:setProperty name="myName" property="someProperty" .../&gt;
</pre>
<p>此时，不管jsp:useBean是找到了一个现有的Bean，还是新创建了一个Bean实例，jsp:setProperty都会执行。第二种用法是把jsp:setProperty放入jsp:useBean元素的内部，如下所示： 
</p>
<pre>
&lt;jsp:useBean id="myName" ... &gt;
...
   &lt;jsp:setProperty name="myName" property="someProperty" .../&gt;
&lt;/jsp:useBean&gt;
</pre>
<p>此时，jsp:setProperty只有在新建Bean实例时才会执行，如果是使用现有实例则不执行jsp:setProperty。 
</p>
<p>jsp:setProperty动作有下面四个属性,如下表：</p>
<table class="reference">
<tbody><tr><th style="width:30%">属性 </th><th>描述 </th></tr>
<tr><td>name</td><td>name属性是必需的。它表示要设置属性的是哪个Bean。  </td></tr>
<tr><td>property</td><td>property属性是必需的。它表示要设置哪个属性。有一个特殊用法：如果property的值是"*"，表示所有名字和Bean属性名字匹配的请求参数都将被传递给相应的属性set方法。 </td></tr>
<tr><td>value</td><td> value 属性是可选的。该属性用来指定Bean属性的值。字符串数据会在目标类中通过标准的valueOf方法自动转换成数字、boolean、Boolean、 byte、Byte、char、Character。例如，boolean和Boolean类型的属性值（比如"true"）通过 Boolean.valueOf转换，int和Integer类型的属性值（比如"42"）通过Integer.valueOf转换。 
　　value和param不能同时使用，但可以使用其中任意一个。 </td></tr>
<tr><td>param</td><td> param 是可选的。它指定用哪个请求参数作为Bean属性的值。如果当前请求没有参数，则什么事情也不做，系统不会把null传递给Bean属性的set方法。因此，你可以让Bean自己提供默认属性值，只有当请求参数明确指定了新值时才修改默认属性值。 </td></tr>
</tbody></table>
<hr>
<h2>&lt;jsp:getProperty&gt;动作元素</h2>
<p>jsp:getProperty动作提取指定Bean属性的值，转换成字符串，然后输出。语法格式如下：</p>
<pre>
&lt;jsp:useBean id="myName" ... /&gt;
...
&lt;jsp:getProperty name="myName" property="someProperty" .../&gt;
</pre>
<p>下表是与getProperty相关联的属性：</p>
<table class="reference">
<tbody><tr><th style="width:30%">属性 </th><th>描述 </th></tr>
<tr><td>name</td><td>要检索的Bean属性名称。Bean必须已定义。</td></tr>
<tr><td>property</td><td>表示要提取Bean属性的值</td></tr>
</tbody></table>
<h3>实例</h3>
<p>以下实例我们使用了Bean:</p>
<pre>
package com.runoob.main;

public class TestBean {
   private String message = "菜鸟教程";
 
   public String getMessage() {
      return(message);
   }
   public void setMessage(String message) {
      this.message = message;
   }
}
</pre>

<p>编译以上实例文件 TestBean.java ：</p>
<pre>
$ javac TestBean.java
</pre>
<p>编译完成后会在当前目录下生成一个 <strong>TestBean.class</strong> 文件，
将该文件拷贝至当前 JSP 项目的 <strong>WebContent/WEB-INF/classes/com/runoob/main</strong>
下( com/runoob/main 包路径，没有需要手动创建)。
</p>
<p>下面是一个 Eclipse 中目录结构图：</p>
<p><img  width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/6AC33FBA-0B76-4BFD-A690-E856E9E01900.jpg"></p>
<p>下面是一个很简单的例子，它的功能是装载一个Bean，然后设置/读取它的message属性。 </p>
</p>
<p>现在让我们在main.jsp文件中调用该Bean:</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h2&gt;Jsp 使用 JavaBean 实例&lt;/h2&gt;
&lt;jsp:useBean id="test" class="com.runoob.main.TestBean" /&gt;
 
&lt;jsp:setProperty name="test" 
                    property="message" 
                    value="菜鸟教程..." /&gt;
 
&lt;p&gt;输出信息....&lt;/p&gt;
 
&lt;jsp:getProperty name="test" property="message" /&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>浏览器访问，执行以上文件，输出如下所示：</p>
<p>
<img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/D7AD87A8-3392-4D4E-8731-18806B0644CD.jpg"></p>
<hr>
<h2>&lt;jsp:forward&gt; 动作元素</h2>
<p>　jsp:forward动作把请求转到另外的页面。jsp:forward标记只有一个属性page。语法格式如下所示： 
</p>
<pre>
&lt;jsp:forward page="相对 URL 地址" /&gt;
</pre>
<p>以下是forward相关联的属性：</p>
<table class="reference">
<tbody><tr><th style="width:30%">属性 </th><th>描述 </th></tr>
<tr><td>page</td><td>page属性包含的是一个相对URL。page的值既可以直接给出，也可以在请求的时候动态计算，可以是一个JSP页面或者一个 Java Servlet.</td></tr>
</tbody></table>
<h3>实例</h3>
<p>以下实例我们使用了两个文件，分别是： date.jsp 和 main.jsp。</p>
<p> date.jsp 文件代码如下：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;p&gt;
   今天的日期是: &lt;%= (new java.util.Date()).toLocaleString()%&gt;
&lt;/p&gt;
</pre>
<p>main.jsp文件代码：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h2&gt;forward 动作实例&lt;/h2&gt;
&lt;jsp:forward page="date.jsp" /&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>现在将以上两个文件放在服务器的根目录下，访问main.jsp文件。显示结果如下：</p>
<pre>
今天的日期是: 2016-6-25 14:37:25
</pre>
<hr>
<h2> &lt;jsp:plugin&gt;动作元素</h2>
<p>jsp:plugin动作用来根据浏览器的类型，插入通过Java插件 运行Java Applet所必需的OBJECT或EMBED元素。</p>
<p>如果需要的插件不存在，它会下载插件，然后执行Java组件。 Java组件可以是一个applet或一个JavaBean。 </p>
<p>plugin动作有多个对应HTML元素的属性用于格式化Java 组件。param元素可用于向Applet 或 Bean 传递参数。</p>
<p>以下是使用plugin 动作元素的典型实例:</p>
<pre>
&lt;jsp:plugin type="applet" codebase="dirname" code="MyApplet.class"
                           width="60" height="80"&gt;
   &lt;jsp:param name="fontcolor" value="red" /&gt;
   &lt;jsp:param name="background" value="black" /&gt;
 
   &lt;jsp:fallback&gt;
      Unable to initialize Java Plugin
   &lt;/jsp:fallback&gt;
 
&lt;/jsp:plugin&gt;
</pre>
<p>如果你有兴趣可以尝试使用applet来测试jsp:plugin动作元素，&lt;fallback&gt;元素是一个新元素，在组件出现故障的错误是发送给用户错误信息。</p>
<hr><h2>
&lt;jsp:element&gt; 、
&lt;jsp:attribute&gt;、
&lt;jsp:body&gt;动作元素</h2>
<p>&lt;jsp:element&gt; 、
&lt;jsp:attribute&gt;、
&lt;jsp:body&gt;动作元素动态定义XML元素。动态是非常重要的，这就意味着XML元素在编译时是动态生成的而非静态。</p>
<p>以下实例动态定义了XML元素：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;jsp:element name="xmlElement"&gt;
&lt;jsp:attribute name="xmlElementAttr"&gt;
   属性值
&lt;/jsp:attribute&gt;
&lt;jsp:body&gt;
   XML 元素的主体
&lt;/jsp:body&gt;
&lt;/jsp:element&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>浏览器访问以下页面，输出结果如下所示：</p>
<p>
<img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/7D8C47F0-0DDE-4F1D-8BE1-B2C9C955683E.jpg">
</p>
<hr>
<h2>&lt;jsp:text&gt;动作元素</h2>
<p>&lt;jsp:text&gt;动作元素允许在JSP页面和文档中使用写入文本的模板，语法格式如下：</p>
<pre>
&lt;jsp:text&gt;模板数据&lt;/jsp:text&gt;
</pre>
<p>以上文本模板不能包含其他元素，只能只能包含文本和EL表达式（注：EL表达式将在后续章节中介绍）。请注意，在XML文件中，您不能使用表达式如 ${whatever &gt; 0}，因为&gt;符号是非法的。
你可以使用 ${whatever gt 0}表达式或者嵌入在一个CDATA部分的值。
</p>
<pre>
&lt;jsp:text&gt;&lt;![CDATA[&lt;br&gt;]]&gt;&lt;/jsp:text&gt;
</pre>
<p>如果你需要在 XHTML 中声明 DOCTYPE,必须使用到&lt;jsp:text&gt;动作元素，实例如下：</p>
<pre>
&lt;jsp:text&gt;&lt;![CDATA[&lt;!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd"&gt;]]&gt;
&lt;/jsp:text&gt;
&lt;head&gt;&lt;title&gt;jsp:text action&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;

&lt;books&gt;&lt;book&gt;&lt;jsp:text&gt;  
    Welcome to JSP Programming
&lt;/jsp:text&gt;&lt;/book&gt;&lt;/books&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>你可以对以上实例尝试使用&lt;jsp:text&gt;及不使用该动作元素执行结果的区别。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-directives.html" rel="prev"> JSP 指令</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-implicit-objects.html" rel="next"> JSP 隐式对象</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-actions.html" rel="prev"> JSP 动作元素</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-client-request.html" rel="next"> JSP 客户端请求</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">隐式对象</span></h1>
<p>JSP隐式对象是JSP容器为每个页面提供的Java对象，开发者可以直接使用它们而不用显式声明。JSP隐式对象也被称为预定义变量。</p>
<p>
JSP所支持的九大隐式对象：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>对象</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				request</td>
			<td>
				<strong>HttpServletRequest</strong>类的实例</td>
		</tr>
		<tr>
			<td>
				response</td>
			<td>
				<strong>HttpServletResponse</strong>类的实例</td>
		</tr>
		<tr>
			<td>
				out</td>
			<td>
				<strong>PrintWriter</strong>类的实例，用于把结果输出至网页上</td>
		</tr>
		<tr>
			<td>
				session</td>
			<td>
				<strong>HttpSession</strong>类的实例</td>
		</tr>
		<tr>
			<td>
				application</td>
			<td>
				<strong>ServletContext</strong>类的实例，与应用上下文有关</td>
		</tr>
		<tr>
			<td>
				config</td>
			<td>
				<strong>ServletConfig</strong>类的实例</td>
		</tr>
		<tr>
			<td>
				pageContext</td>
			<td>
				<strong>PageContext</strong>类的实例，提供对JSP页面所有对象以及命名空间的访问</td>
		</tr>
		<tr>
			<td>
				page</td>
			<td>
				类似于Java类中的this关键字</td>
		</tr>
		<tr>
			<td>
				Exception</td>
			<td>
				<strong>Exception</strong>类的对象，代表发生错误的JSP页面中对应的异常对象</td>
		</tr>
	</tbody>
</table>
<hr>
<h2>
	request对象</h2><p>
request对象是javax.servlet.http.HttpServletRequest 类的实例。每当客户端请求一个JSP页面时，JSP引擎就会制造一个新的request对象来代表这个请求。</p>
<p>
request对象提供了一系列方法来获取HTTP头信息，cookies，HTTP方法等等。</p>
<hr>
<h2>
	response对象</h2><p>
response对象是javax.servlet.http.HttpServletResponse类的实例。当服务器创建request对象时会同时创建用于响应这个客户端的response对象。</p>
<p>
response对象也定义了处理HTTP头模块的接口。通过这个对象，开发者们可以添加新的cookies，时间戳，HTTP状态码等等。</p>
<hr>
<h2>
	out对象</h2><p>
out对象是 javax.servlet.jsp.JspWriter 类的实例，用来在response对象中写入内容。</p>
<p>
最初的JspWriter类对象根据页面是否有缓存来进行不同的实例化操作。可以在page指令中使用buffered='false'属性来轻松关闭缓存。</p>
<p>
JspWriter类包含了大部分java.io.PrintWriter类中的方法。不过，JspWriter新增了一些专为处理缓存而设计的方法。还有就是，JspWriter类会抛出IOExceptions异常，而PrintWriter不会。</p>
<p>
下表列出了我们将会用来输出boolean，char，int，double，String，object等类型数据的重要方法：</p>
<table  class="reference">
	<tbody>
		<tr>
			<th>
				<strong>方法</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				<strong>out.print(dataType dt)</strong></td>
			<td>
				输出Type类型的值</td>
		</tr>
		<tr>
			<td>
				<strong>out.println(dataType dt)</strong></td>
			<td>
				输出Type类型的值然后换行</td>
		</tr>
		<tr>
			<td>
				<strong>out.flush()</strong></td>
			<td>
				刷新输出流</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	session对象</h2><p>
session对象是 javax.servlet.http.HttpSession 类的实例。和Java Servlets中的session对象有一样的行为。</p>
<p>
session对象用来跟踪在各个客户端请求间的会话。</p>
<hr>
<h2>
	application对象</h2><p>
application对象直接包装了servlet的ServletContext类的对象，是javax.servlet.ServletContext 类的实例。</p>
<p>
这个对象在JSP页面的整个生命周期中都代表着这个JSP页面。这个对象在JSP页面初始化时被创建，随着jspDestroy()方法的调用而被移除。</p>
<p>
通过向application中添加属性，则所有组成您web应用的JSP文件都能访问到这些属性。</p>
<hr>
<h2>
config对象</h2><p>
config对象是 javax.servlet.ServletConfig 类的实例，直接包装了servlet的ServletConfig类的对象。</p>
<p>
这个对象允许开发者访问Servlet或者JSP引擎的初始化参数，比如文件路径等。</p>
<p>
以下是config对象的使用方法，不是很重要，所以不常用：
<pre>
config.getServletName();
</pre><p>
它返回包含在&lt;servlet-name&gt;元素中的servlet名字，注意，&lt;servlet-name&gt;元素在 WEB-INF\web.xml 文件中定义。</p><hr>
<h2>
	pageContext 对象</h2><p>
pageContext对象是javax.servlet.jsp.PageContext 类的实例，用来代表整个JSP页面。</p>
<p>
这个对象主要用来访问页面信息，同时过滤掉大部分实现细节。</p>
<p>
这个对象存储了request对象和response对象的引用。application对象，config对象，session对象，out对象可以通过访问这个对象的属性来导出。</p>
<p>
pageContext对象也包含了传给JSP页面的指令信息，包括缓存信息，ErrorPage URL,页面scope等。</p>
<p>
PageContext类定义了一些字段，包括PAGE_SCOPE，REQUEST_SCOPE，SESSION_SCOPE， APPLICATION_SCOPE。它也提供了40余种方法，有一半继承自javax.servlet.jsp.JspContext 类。</p>
<p>
其中一个重要的方法就是removeArribute()，它可接受一个或两个参数。比如，pageContext.removeArribute("attrName")移除四个scope中相关属性，但是下面这种方法只移除特定scope中的相关属性：
<pre>
pageContext.removeAttribute("attrName", PAGE_SCOPE);
</pre>
<hr>
<h2>
	page 对象</h2><p>
这个对象就是页面实例的引用。它可以被看做是整个JSP页面的代表。</p>
<p>
page 对象就是this对象的同义词。</p><hr>
<h2>
	exception 对象</h2><p>
exception 对象包装了从先前页面中抛出的异常信息。它通常被用来产生对出错条件的适当响应。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-actions.html" rel="prev"> JSP 动作元素</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-client-request.html" rel="next"> JSP 客户端请求</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-implicit-objects.html" rel="prev"> JSP 隐式对象</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-server-response.html" rel="next"> JSP 服务器响应</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">客户端请求</span></h1>
 <p>当浏览器请求一个网页时，它会向网络服务器发送一系列不能被直接读取的信息，因为这些信息是作为HTTP信息头的一部分来传送的。您可以查阅HTTP协议来获得更多的信息。</p>
 <p>
下表列出了浏览器端信息头的一些重要内容，在以后的网络编程中将会经常见到这些信息：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>信息</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td style="width:157px;">
				Accept</td>
			<td style="width:403px;">
				指定浏览器或其他客户端可以处理的MIME类型。它的值通常为&nbsp;<strong>image/png</strong>&nbsp;或&nbsp;<strong>image/jpeg</strong></td>
		</tr>
		<tr>
			<td style="width:157px;">
				Accept-Charset</td>
			<td style="width:403px;">
				指定浏览器要使用的字符集。比如 ISO-8859-1</td>
		</tr>
		<tr>
			<td style="width:157px;">
				Accept-Encoding</td>
			<td style="width:403px;">
				指定编码类型。它的值通常为&nbsp;<strong>gzip</strong>&nbsp;或<strong>compress</strong></td>
		</tr>
		<tr>
			<td style="width:157px;">
				Accept-Language</td>
			<td style="width:403px;">
				指定客户端首选语言，servlet会优先返回以当前语言构成的结果集，如果servlet支持这种语言的话。比如 en，en-us，ru等等</td>
		</tr>
		<tr>
			<td style="width:157px;">
				Authorization</td>
			<td style="width:403px;">
				在访问受密码保护的网页时识别不同的用户</td>
		</tr>
		<tr>
			<td style="width:157px;">
				Connection</td>
			<td style="width:403px;">
				表明客户端是否可以处理HTTP持久连接。持久连接允许客户端或浏览器在一个请求中获取多个文件。<strong>Keep-Alive</strong>&nbsp;表示启用持久连接</td>
		</tr>
		<tr>
			<td style="width:157px;">
				Content-Length</td>
			<td style="width:403px;">
				仅适用于POST请求，表示 POST 数据的字节数</td>
		</tr>
		<tr>
			<td style="width:157px;">
				Cookie</td>
			<td style="width:403px;">
				返回先前发送给浏览器的cookies至服务器</td>
		</tr>
		<tr>
			<td style="width:157px;">
				Host</td>
			<td style="width:403px;">
				指出原始URL中的主机名和端口号</td>
		</tr>
		<tr>
			<td style="width:157px;">
				If-Modified-Since</td>
			<td style="width:403px;">
				表明只有当网页在指定的日期被修改后客户端才需要这个网页。 服务器发送304码给客户端，表示没有更新的资源</td>
		</tr>
		<tr>
			<td style="width:157px;">
				If-Unmodified-Since</td>
			<td style="width:403px;">
				与If-Modified-Since相反， 只有文档在指定日期后仍未被修改过，操作才会成功</td>
		</tr>
		<tr>
			<td style="width:157px;">
				Referer</td>
			<td style="width:403px;">
				标志着所引用页面的URL。比如，如果你在页面1，然后点了个链接至页面2，那么页面1的URL就会包含在浏览器请求页面2的信息头中</td>
		</tr>
		<tr>
			<td style="width:157px;">
				User-Agent</td>
			<td style="width:403px;">
				用来区分不同浏览器或客户端发送的请求，并对不同类型的浏览器返回不同的内容</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	HttpServletRequest类</h2> <p>
request对象是javax.servlet.http.HttpServletRequest类的实例。每当客户端请求一个页面时，JSP引擎就会产生一个新的对象来代表这个请求。</p>
 <p>
request对象提供了一系列方法来获取HTTP信息头，包括表单数据，cookies，HTTP方法等等。</p>
 <p>
接下来将会介绍一些在JSP编程中常用的获取HTTP信息头的方法。详细内容请见下表：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>序号</strong></th>
			<th>
				<strong>方法</strong><strong>&amp; </strong><strong>描述</strong></th>
		</tr>
		<tr>
			<td style="width:43px;">
				1</td>
			<td style="width:517px;">
				<strong>Cookie[] getCookies()</strong></p>
				返回客户端所有的Cookie的数组</td>
		</tr>
		<tr>
			<td style="width:43px;">
				2</td>
			<td style="width:517px;">
				<strong>Enumeration getAttributeNames()</strong></p>
				返回request对象的所有属性名称的集合</td>
		</tr>
		<tr>
			<td style="width:43px;">
				3</td>
			<td style="width:517px;">
				<strong>Enumeration getHeaderNames()</strong></p>
				返回所有HTTP头的名称集合</td>
		</tr>
		<tr>
			<td style="width:43px;">
				4</td>
			<td style="width:517px;">
				<strong>Enumeration getParameterNames()</strong></p>
				返回请求中所有参数的集合</td>
		</tr>
		<tr>
			<td style="width:43px;">
				5</td>
			<td style="width:517px;">
				<strong>HttpSession getSession()</strong></p>
				返回request对应的session对象，如果没有，则创建一个</td>
		</tr>
		<tr>
			<td style="width:43px;">
				6</td>
			<td style="width:517px;">
				<strong>HttpSession getSession(boolean create)</strong></p>
				返回request对应的session对象，如果没有并且参数create为true，则返回一个新的session对象</td>
		</tr>
		<tr>
			<td style="width:43px;">
				7</td>
			<td style="width:517px;">
				<strong>Locale getLocale()</strong></p>
				返回当前页的Locale对象，可以在response中设置</td>
		</tr>
		<tr>
			<td style="width:43px;">
				8</td>
			<td style="width:517px;">
				<strong>Object getAttribute(String name)</strong></p>
				返回名称为name的属性值，如果不存在则返回null。</td>
		</tr>
		<tr>
			<td style="width:43px;">
				9</td>
			<td style="width:517px;">
				<strong>ServletInputStream getInputStream()</strong></p>
				返回请求的输入流</td>
		</tr>
		<tr>
			<td style="width:43px;">
				10</td>
			<td style="width:517px;">
				<strong>String getAuthType()</strong></p>
				返回认证方案的名称，用来保护servlet，比如 "BASIC" 或者 "SSL" 或 null 如果 JSP没设置保护措施</td>
		</tr>
		<tr>
			<td style="width:43px;">
				11</td>
			<td style="width:517px;">
				<strong>String getCharacterEncoding()</strong></p>
				返回request的字符编码集名称</td>
		</tr>
		<tr>
			<td style="width:43px;">
				12</td>
			<td style="width:517px;">
				<strong>String getContentType()</strong></p>
				返回request主体的MIME类型，若未知则返回null</td>
		</tr>
		<tr>
			<td style="width:43px;">
				13</td>
			<td style="width:517px;">
				<strong>String getContextPath()</strong></p>
				返回request URI中指明的上下文路径</td>
		</tr>
		<tr>
			<td style="width:43px;">
				14</td>
			<td style="width:517px;">
				<strong>String getHeader(String name)</strong></p>
				返回name指定的信息头</td>
		</tr>
		<tr>
			<td style="width:43px;">
				15</td>
			<td style="width:517px;">
				<strong>String getMethod()</strong></p>
				返回此request中的HTTP方法，比如 GET,，POST，或PUT</td>
		</tr>
		<tr>
			<td style="width:43px;">
				16</td>
			<td style="width:517px;">
				<strong>String getParameter(String name)</strong></p>
				返回此request中name指定的参数，若不存在则返回null</td>
		</tr>
		<tr>
			<td style="width:43px;">
				17</td>
			<td style="width:517px;">
				<strong>String getPathInfo()</strong></p>
				返回任何额外的与此request URL相关的路径</td>
		</tr>
		<tr>
			<td style="width:43px;">
				18</td>
			<td style="width:517px;">
				<strong>String getProtocol()</strong></p>
				返回此request所使用的协议名和版本</td>
		</tr>
		<tr>
			<td style="width:43px;">
				19</td>
			<td style="width:517px;">
				<strong>String getQueryString()</strong></p>
				返回此 request URL包含的查询字符串</td>
		</tr>
		<tr>
			<td style="width:43px;">
				20</td>
			<td style="width:517px;">
				<strong>String getRemoteAddr()</strong></p>
				返回客户端的IP地址</td>
		</tr>
		<tr>
			<td style="width:43px;">
				21</td>
			<td style="width:517px;">
				<strong>String getRemoteHost()</strong></p>
				返回客户端的完整名称</td>
		</tr>
		<tr>
			<td style="width:43px;">
				22</td>
			<td style="width:517px;">
				<strong>String getRemoteUser()</strong></p>
				返回客户端通过登录认证的用户，若用户未认证则返回null</td>
		</tr>
		<tr>
			<td style="width:43px;">
				23</td>
			<td style="width:517px;">
				<strong>String getRequestURI()</strong></p>
				返回request的URI</td>
		</tr>
		<tr>
			<td style="width:43px;">
				24</td>
			<td style="width:517px;">
				<strong>String getRequestedSessionId()</strong></p>
				返回request指定的session ID</td>
		</tr>
		<tr>
			<td style="width:43px;">
				25</td>
			<td style="width:517px;">
				<strong>String getServletPath()</strong></p>
				返回所请求的servlet路径</td>
		</tr>
		<tr>
			<td style="width:43px;">
				26</td>
			<td style="width:517px;">
				<strong>String[] getParameterValues(String name)</strong></p>
				返回指定名称的参数的所有值，若不存在则返回null</td>
		</tr>
		<tr>
			<td style="width:43px;">
				27</td>
			<td style="width:517px;">
				<strong>boolean isSecure()</strong></p>
				返回request是否使用了加密通道，比如HTTPS</td>
		</tr>
		<tr>
			<td style="width:43px;">
				28</td>
			<td style="width:517px;">
				<strong>int getContentLength()</strong></p>
				返回request主体所包含的字节数，若未知的返回-1</td>
		</tr>
		<tr>
			<td style="width:43px;">
				29</td>
			<td style="width:517px;">
				<strong>int getIntHeader(String name)</strong></p>
				返回指定名称的request信息头的值</td>
		</tr>
		<tr>
			<td style="width:43px;">
				30</td>
			<td style="width:517px;">
				<strong>int getServerPort()</strong></p>
				返回服务器端口号</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	HTTP信息头示例</h2> <p>
在这个例子中，我们会使用HttpServletRequest类的getHeaderNames()方法来读取HTTP信息头。这个方法以枚举的形式返回当前HTTP请求的头信息。</p>
 <p>
获取Enumeration对象后，用标准的方式来遍历Enumeration对象，用hasMoreElements()方法来确定什么时候停止，用nextElement()方法来获得每个参数的名字。</p>


<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;HTTP 头部请求实例&lt;/h2&gt;
&lt;table width="100%" border="1" align="center"&gt;
&lt;tr bgcolor="#949494"&gt;
&lt;th&gt;Header Name&lt;/th&gt;&lt;th&gt;Header Value(s)&lt;/th&gt;
&lt;/tr&gt;
&lt;%
   Enumeration headerNames = request.getHeaderNames();
   while(headerNames.hasMoreElements()) {
      String paramName = (String)headerNames.nextElement();
      out.print("&lt;tr&gt;&lt;td&gt;" + paramName + "&lt;/td&gt;\n");
      String paramValue = request.getHeader(paramName);
      out.println("&lt;td&gt; " + paramValue + "&lt;/td&gt;&lt;/tr&gt;\n");
   }
%&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
访问main.jsp，将会得到以下结果：</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/jspheadmsg.jpg"></p>
<p>
您可以在上面代码中尝试HttpServletRequest类的其它方法。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-implicit-objects.html" rel="prev"> JSP 隐式对象</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-server-response.html" rel="next"> JSP 服务器响应</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-client-request.html" rel="prev"> JSP 客户端请求</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-http-status-codes.html" rel="next"> JSP HTTP 状态码</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">服务器响应</span></h1>
<p>Response响应对象主要将JSP容器处理后的结果传回到客户端。可以通过response变量设置HTTP的状态和向客户端发送数据，如Cookie、HTTP文件头信息等。</p>
<p>
一个典型的响应看起来就像下面这样：</p>
<pre>
HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
  (空行)
&lt;!doctype ...&gt;
&lt;html&gt;
&lt;head&gt;...&lt;/head&gt;
&lt;body&gt;
...
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
状态行包含HTTP版本信息，比如HTTP/1.1，一个状态码，比如200，还有一个非常短的信息对应着状态码，比如OK。</p>
<p>
下表摘要出了HTTP1.1响应头中最有用的部分，在网络编程中您将会经常见到它们：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>响应头</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				Allow</td>
			<td>
				指定服务器支持的request方法（GET，POST等等）</td>
		</tr>
		<tr>
			<td>
				Cache-Control</td>
			<td>
				指定响应文档能够被安全缓存的情况。通常取值为&nbsp;<strong>public</strong><strong>，</strong><strong>private</strong>&nbsp;或<strong>no-cache</strong>&nbsp;等等。 Public意味着文档可缓存，Private意味着文档只为单用户服务并且只能使用私有缓存。No-cache 意味着文档不被缓存。</td>
		</tr>
		<tr>
			<td>
				Connection</td>
			<td>
				命令浏览器是否要使用持久的HTTP连接。<strong>close</strong><strong>值</strong>&nbsp;命令浏览器不使用持久HTTP连接，而keep-alive 意味着使用持久化连接。</td>
		</tr>
		<tr>
			<td>
				Content-Disposition</td>
			<td>
				让浏览器要求用户将响应以给定的名称存储在磁盘中</td>
		</tr>
		<tr>
			<td>
				Content-Encoding</td>
			<td>
				指定传输时页面的编码规则</td>
		</tr>
		<tr>
			<td>
				Content-Language</td>
			<td>
				表述文档所使用的语言，比如en， en-us,，ru等等</td>
		</tr>
		<tr>
			<td>
				Content-Length</td>
			<td>
				表明响应的字节数。只有在浏览器使用持久化 (keep-alive) HTTP 连接时才有用</td>
		</tr>
		<tr>
			<td>
				Content-Type</td>
			<td>
				表明文档使用的MIME类型</td>
		</tr>
		<tr>
			<td>
				Expires</td>
			<td>
				指明啥时候过期并从缓存中移除</td>
		</tr>
		<tr>
			<td>
				Last-Modified</td>
			<td>
				指明文档最后修改时间。客户端可以 缓存文档并且在后续的请求中提供一个&nbsp;<strong>If-Modified-Since</strong>请求头</td>
		</tr>
		<tr>
			<td>
				Location</td>
			<td>
				在300秒内，包含所有的有一个状态码的响应地址，浏览器会自动重连然后检索新文档</td>
		</tr>
		<tr>
			<td>
				Refresh</td>
			<td>
				指明浏览器每隔多久请求更新一次页面。</td>
		</tr>
		<tr>
			<td>
				Retry-After</td>
			<td>
				与503 (Service Unavailable)一起使用来告诉用户多久后请求将会得到响应</td>
		</tr>
		<tr>
			<td>
				Set-Cookie</td>
			<td>
				指明当前页面对应的cookie</td>
		</tr>
	</tbody>
</table><hr>
<h2>HttpServletResponse类</h2><p>
response 对象是 javax.servlet.http.HttpServletResponse 类的一个实例。就像服务器会创建request对象一样，它也会创建一个客户端响应。</p>
<p>
response对象定义了处理创建HTTP信息头的接口。通过使用这个对象，开发者们可以添加新的cookie或时间戳，还有HTTP状态码等等。</p>
<p>
下表列出了用来设置HTTP响应头的方法，这些方法由HttpServletResponse 类提供：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>S.N.</strong></th>
			<th>
				<strong>方法</strong><strong> &amp; </strong><strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				1</td>
			<td>
				<strong>String encodeRedirectURL(String url)</strong></p>
				对sendRedirect()方法使用的URL进行编码</td>
		</tr>
		<tr>
			<td>
				2</td>
			<td>
				<strong>String encodeURL(String url)</strong></p>
				将URL编码，回传包含Session ID的URL</td>
		</tr>
		<tr>
			<td>
				3</td>
			<td>
				<strong>boolean containsHeader(String name)</strong></p>
				返回指定的响应头是否存在</td>
		</tr>
		<tr>
			<td>
				4</td>
			<td>
				<strong>boolean isCommitted()</strong></p>
				返回响应是否已经提交到客户端</td>
		</tr>
		<tr>
			<td>
				5</td>
			<td>
				<strong>void addCookie(Cookie cookie)</strong></p>
				添加指定的cookie至响应中</td>
		</tr>
		<tr>
			<td>
				6</td>
			<td>
				<strong>void addDateHeader(String name, long date)</strong></p>
				添加指定名称的响应头和日期值</td>
		</tr>
		<tr>
			<td>
				7</td>
			<td>
				<strong>void addHeader(String name, String value)</strong></p>
				添加指定名称的响应头和值</td>
		</tr>
		<tr>
			<td>
				8</td>
			<td>
				<strong>void addIntHeader(String name, int value)</strong></p>
				添加指定名称的响应头和int值</td>
		</tr>
		<tr>
			<td>
				9</td>
			<td>
				<strong>void flushBuffer()</strong></p>
				将任何缓存中的内容写入客户端</td>
		</tr>
		<tr>
			<td>
				10</td>
			<td>
				<strong>void reset()</strong></p>
				清除任何缓存中的任何数据，包括状态码和各种响应头</td>
		</tr>
		<tr>
			<td>
				11</td>
			<td>
				<strong>void resetBuffer()</strong></p>
				清除基本的缓存数据，不包括响应头和状态码</td>
		</tr>
		<tr>
			<td>
				12</td>
			<td>
				<strong>void sendError(int sc)</strong></p>
				使用指定的状态码向客户端发送一个出错响应，然后清除缓存</td>
		</tr>
		<tr>
			<td>
				13</td>
			<td>
				<strong>void sendError(int sc, String msg)</strong></p>
				使用指定的状态码和消息向客户端发送一个出错响应</td>
		</tr>
		<tr>
			<td>
				14</td>
			<td>
				<strong>void sendRedirect(String location)</strong></p>
				使用指定的URL向客户端发送一个临时的间接响应</td>
		</tr>
		<tr>
			<td>
				15</td>
			<td>
				<strong>void setBufferSize(int size)</strong></p>
				设置响应体的缓存区大小</td>
		</tr>
		<tr>
			<td>
				16</td>
			<td>
				<strong>void setCharacterEncoding(String charset)</strong></p>
				指定响应的编码集（MIME字符集），例如UTF-8</td>
		</tr>
		<tr>
			<td>
				17</td>
			<td>
				<strong>void setContentLength(int len)</strong></p>
				指定HTTP servlets中响应的内容的长度，此方法用来设置 HTTP Content-Length 信息头</td>
		</tr>
		<tr>
			<td>
				18</td>
			<td>
				<strong>void setContentType(String type)</strong></p>
				设置响应的内容的类型，如果响应还未被提交的话</td>
		</tr>
		<tr>
			<td>
				19</td>
			<td>
				<strong>void setDateHeader(String name, long date)</strong></p>
				使用指定名称和值设置响应头的名称和内容</td>
		</tr>
		<tr>
			<td>
				20</td>
			<td>
				<strong>void setHeader(String name, String value)</strong></p>
				使用指定名称和值设置响应头的名称和内容</td>
		</tr>
		<tr>
			<td>
				21</td>
			<td>
				<strong>void setIntHeader(String name, int value)</strong></p>
				使用指定名称和值设置响应头的名称和内容</td>
		</tr>
		<tr>
			<td>
				22</td>
			<td>
				<strong>void setLocale(Locale loc)</strong></p>
				设置响应的语言环境，如果响应尚未被提交的话</td>
		</tr>
		<tr>
			<td>
				23</td>
			<td>
				<strong>void setStatus(int sc)</strong></p>
				设置响应的状态码</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	HTTP响应头程序示例</h2><p>
接下来的例子使用setIntHeader()方法和setRefreshHeader()方法来模拟一个数字时钟：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h2&gt;自动刷新实例&lt;/h2&gt;
&lt;%
   // 设置每隔5秒自动刷新
   response.setIntHeader("Refresh", 5);
   // 获取当前时间
   Calendar calendar = new GregorianCalendar();
   String am_pm;
   int hour = calendar.get(Calendar.HOUR);
   int minute = calendar.get(Calendar.MINUTE);
   int second = calendar.get(Calendar.SECOND);
   if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
   else
      am_pm = "PM";
   String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
   out.println("当前时间: " + CT + "\n");
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
将以上代码保存为main.jsp，然后通过浏览器访问它。它将会每隔5秒显示一下系统当前时间。</p>
<p>我们可以看下以下 Gif 演示图：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp1.gif"></p>
<p>您也可以自己动手修改以上代码，试试使用其他的方法，将能得到更深的体会。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-client-request.html" rel="prev"> JSP 客户端请求</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-http-status-codes.html" rel="next"> JSP HTTP 状态码</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-server-response.html" rel="prev"> JSP 服务器响应</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-form-processing.html" rel="next"> JSP 表单处理</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">HTTP 状态码</span></h1>
            <p>HTTP请求与HTTP响应的格式相近，都有着如下结构：</p>
<ul>
	<li>
		以状态行+CRLF（回车换行）开始</li>
	<li>
		零行或多行头模块+CRLF</li>
	<li>
		一个空行，比如CRLF</li>
	<li>
		可选的消息体比如文件，查询数据，查询输出</li>
</ul> <p>
举例来说，一个服务器响应头看起来就像下面这样： </p>
<pre>
HTTP/1.1 200 OK
Content-Type: text/html
Header2: ...
...
HeaderN: ...
  (Blank Line)
&lt;!doctype ...&gt;
&lt;html&gt;
&lt;head&gt;...&lt;/head&gt;
&lt;body&gt;
...
&lt;/body&gt;
&lt;/html&gt;
</pre>
</p>
状态行包含HTTP版本，一个状态码，和状态码相对应的短消息。</p>
<p>
下表列出了可能会从服务器返回的HTTP状态码和与之关联的消息：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>状态码</strong></th>
			<th>
				<strong>消息</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td style="width:52px;">
				100</td>
			<td style="width:115px;">
				Continue</td>
			<td>
				只有一部分请求被服务器接收，但只要没被服务器拒绝，客户端就会延续这个请求</td>
		</tr>
		<tr>
			<td style="width:52px;">
				101</td>
			<td style="width:115px;">
				Switching Protocols</td>
			<td>
				服务器交换机协议</td>
		</tr>
		<tr>
			<td style="width:52px;">
				200</td>
			<td style="width:115px;">
				OK</td>
			<td>
				请求被确认</td>
		</tr>
		<tr>
			<td style="width:52px;">
				201</td>
			<td style="width:115px;">
				Created</td>
			<td>
				请求时完整的，新的资源被创建</td>
		</tr>
		<tr>
			<td style="width:52px;">
				202</td>
			<td style="width:115px;">
				Accepted</td>
			<td>
				请求被接受，但未处理完</td>
		</tr>
		<tr>
			<td style="width:52px;">
				203</td>
			<td style="width:115px;">
				Non-authoritative Information</td>
			<td>
				&nbsp;</td>
		</tr>
		<tr>
			<td style="width:52px;">
				204</td>
			<td style="width:115px;">
				No Content</td>
			<td>
				&nbsp;</td>
		</tr>
		<tr>
			<td style="width:52px;">
				205</td>
			<td style="width:115px;">
				Reset Content</td>
			<td>
				&nbsp;</td>
		</tr>
		<tr>
			<td style="width:52px;">
				206</td>
			<td style="width:115px;">
				Partial Content</td>
			<td>
				&nbsp;</td>
		</tr>
		<tr>
			<td style="width:52px;">
				300</td>
			<td style="width:115px;">
				Multiple Choices</td>
			<td>
				一个超链接表，用户可以选择一个超链接并访问，最大支持5个超链接</td>
		</tr>
		<tr>
			<td style="width:52px;">
				301</td>
			<td style="width:115px;">
				Moved Permanently</td>
			<td>
				被请求的页面已经移动到了新的URL下</td>
		</tr>
		<tr>
			<td style="width:52px;">
				302</td>
			<td style="width:115px;">
				Found</td>
			<td>
				被请求的页面暂时性地移动到了新的URL下</td>
		</tr>
		<tr>
			<td style="width:52px;">
				303</td>
			<td style="width:115px;">
				See Other</td>
			<td>
				被请求的页面可以在一个不同的URL下找到</td>
		</tr>
		<tr>
			<td style="width:52px;">
				304</td>
			<td style="width:115px;">
				Not Modified</td>
			<td>
				&nbsp;</td>
		</tr>
		<tr>
			<td style="width:52px;">
				305</td>
			<td style="width:115px;">
				Use Proxy</td>
			<td>
				&nbsp;</td>
		</tr>
		<tr>
			<td style="width:52px;">
				306</td>
			<td style="width:115px;">
				<em>Unused</em></td>
			<td>
				已经不再使用此状态码，但状态码被保留</td>
		</tr>
		<tr>
			<td style="width:52px;">
				307</td>
			<td style="width:115px;">
				Temporary Redirect</td>
			<td>
				被请求的页面暂时性地移动到了新的URL下</td>
		</tr>
		<tr>
			<td style="width:52px;">
				400</td>
			<td style="width:115px;">
				Bad Request</td>
			<td>
				服务器无法识别请求</td>
		</tr>
		<tr>
			<td style="width:52px;">
				401</td>
			<td style="width:115px;">
				Unauthorized</td>
			<td>
				被请求的页面需要用户名和密码</td>
		</tr>
		<tr>
			<td style="width:52px;">
				402</td>
			<td style="width:115px;">
				Payment Required</td>
			<td>
				<em>目前还不能使用此状态码</em></td>
		</tr>
		<tr>
			<td style="width:52px;">
				403</td>
			<td style="width:115px;">
				Forbidden</td>
			<td>
				禁止访问所请求的页面</td>
		</tr>
		<tr>
			<td style="width:52px;">
				404</td>
			<td style="width:115px;">
				Not Found</td>
			<td>
				服务器无法找到所请求的页面</td>
		</tr>
		<tr>
			<td style="width:52px;">
				405</td>
			<td style="width:115px;">
				Method Not Allowed</td>
			<td>
				请求中所指定的方法不被允许</td>
		</tr>
		<tr>
			<td style="width:52px;">
				406</td>
			<td style="width:115px;">
				Not Acceptable</td>
			<td>
				服务器只能创建一个客户端无法接受的响应</td>
		</tr>
		<tr>
			<td style="width:52px;">
				407</td>
			<td style="width:115px;">
				Proxy Authentication Required</td>
			<td>
				在请求被服务前必须认证一个代理服务器</td>
		</tr>
		<tr>
			<td style="width:52px;">
				408</td>
			<td style="width:115px;">
				Request Timeout</td>
			<td>
				请求时间超过了服务器所能等待的时间，连接被断开</td>
		</tr>
		<tr>
			<td style="width:52px;">
				409</td>
			<td style="width:115px;">
				Conflict</td>
			<td>
				请求有矛盾的地方</td>
		</tr>
		<tr>
			<td style="width:52px;">
				410</td>
			<td style="width:115px;">
				Gone</td>
			<td>
				被请求的页面不再可用</td>
		</tr>
		<tr>
			<td style="width:52px;">
				411</td>
			<td style="width:115px;">
				Length Required</td>
			<td>
				"Content-Length"没有被定义，服务器拒绝接受请求</td>
		</tr>
		<tr>
			<td style="width:52px;">
				412</td>
			<td style="width:115px;">
				Precondition Failed</td>
			<td>
				请求的前提条件被服务器评估为false</td>
		</tr>
		<tr>
			<td style="width:52px;">
				413</td>
			<td style="width:115px;">
				Request Entity Too Large</td>
			<td>
				因为请求的实体太大，服务器拒绝接受请求</td>
		</tr>
		<tr>
			<td style="width:52px;">
				414</td>
			<td style="width:115px;">
				Request-url Too Long</td>
			<td>
				服务器拒绝接受请求，因为URL太长。多出现在把"POST"请求转换为"GET"请求时所附带的大量查询信息</td>
		</tr>
		<tr>
			<td style="width:52px;">
				415</td>
			<td style="width:115px;">
				Unsupported Media Type</td>
			<td>
				服务器拒绝接受请求，因为媒体类型不被支持</td>
		</tr>
		<tr>
			<td style="width:52px;">
				417</td>
			<td style="width:115px;">
				Expectation Failed</td>
			<td>
				&nbsp;</td>
		</tr>
		<tr>
			<td style="width:52px;">
				500</td>
			<td style="width:115px;">
				Internal Server Error</td>
			<td>
				请求不完整，服务器遇见了出乎意料的状况</td>
		</tr>
		<tr>
			<td style="width:52px;">
				501</td>
			<td style="width:115px;">
				Not Implemented</td>
			<td>
				请求不完整，服务器不提供所需要的功能</td>
		</tr>
		<tr>
			<td style="width:52px;">
				502</td>
			<td style="width:115px;">
				Bad Gateway</td>
			<td>
				请求不完整，服务器从上游服务器接受了一个无效的响应</td>
		</tr>
		<tr>
			<td style="width:52px;">
				503</td>
			<td style="width:115px;">
				Service Unavailable</td>
			<td>
				请求不完整，服务器暂时重启或关闭</td>
		</tr>
		<tr>
			<td style="width:52px;">
				504</td>
			<td style="width:115px;">
				Gateway Timeout</td>
			<td>
				网关超时</td>
		</tr>
		<tr>
			<td style="width:52px;">
				505</td>
			<td style="width:115px;">
				HTTP Version Not Supported</td>
			<td>
				服务器不支持所指定的HTTP版本</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	设置HTTP状态码的方法</h2><p>
下表列出了HttpServletResponse 类中用来设置状态码的方法：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>S.N.</strong></th>
			<th>
				<strong>方法</strong><strong> &amp; </strong><strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				1</td>
			<td>
				<strong>public void setStatus ( int statusCode )</strong></p>
				此方法可以设置任意的状态码。如果您的响应包含一个特殊的状态码和一个文档，请确保在用PrintWriter返回任何内容前调用setStatus方法</td>
		</tr>
		<tr>
			<td>
				2</td>
			<td>
				<strong>public void sendRedirect(String url)</strong></p>
				此方法产生302响应，同时产生一个&nbsp;<em>Location</em>&nbsp;头告诉URL 一个新的文档</td>
		</tr>
		<tr>
			<td>
				3</td>
			<td>
				<strong>public void sendError(int code, String message)</strong></p>
				此方法将一个状态码(通常为 404)和一个短消息，自动插入HTML文档中并发回给客户端</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	HTTP状态码程序示例</h2><p>
接下来的例子将会发送407错误码给浏览器，然后浏览器将会告诉您"Need authentication!!!"。</p>
<pre>
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Setting HTTP Status Code&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
   // 设置错误代码，并说明原因
   response.sendError(407, "Need authentication!!!" );
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
访问以上JSP页面，将会得到以下结果：</p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/js_http_status_codes.jpg" alt="js_http_status_codes" width="563" height="162" class="alignnone size-full wp-image-5191" srcset="http://www.runoob.com/wp-content/uploads/2014/01/js_http_status_codes.jpg 563w, http://www.runoob.com/wp-content/uploads/2014/01/js_http_status_codes-300x86.jpg 300w" sizes="(max-width: 563px) 100vw, 563px" />
<p>您也可以试试使用其他的状态码，看会不会得到什么意想不到结果。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-server-response.html" rel="prev"> JSP 服务器响应</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-form-processing.html" rel="next"> JSP 表单处理</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-http-status-codes.html" rel="prev"> JSP HTTP 状态码</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-writing-filters.html" rel="next"> JSP 过滤器</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">表单处理</span></h1>
<p>我们在浏览网页的时候，经常需要向服务器提交信息，并让后台程序处理。浏览器中使用 GET 和 POST 方法向服务器提交数据。</p>
<hr>
<h2>GET 方法</h2>
<p>GET方法将请求的编码信息添加在网址后面，网址与编码信息通过"?"号分隔。如下所示：</p>
<pre>http://www.runoob.com/hello?key1=value1&amp;key2=value2</pre>
<p>GET方法是浏览器默认传递参数的方法，一些敏感信息，如密码等建议不使用GET方法。</p>
<p>用get时，传输数据的大小有限制 （注意不是参数的个数有限制），最大为1024字节。</p>
<hr>
<h2>POST 方法</h2>
<p>一些敏感信息，如密码等我们可以通过POST方法传递，POST提交数据是隐式的。</p>
<p>POST提交数据是不可见的，GET是通过在url里面传递的（可以看一下你浏览器的地址栏）。</p>
<p>JSP使用getParameter()来获得传递的参数，getInputStream()方法用来处理客户端的二进制数据流的请求。</p><hr>

<h2>JSP 读取表单数据</h2>
<ul>
<li><p><b>getParameter():</b> 使用 request.getParameter() 方法来获取表单参数的值。</p></li>
<li><p><b>getParameterValues():</b> 获得如checkbox类（名字相同，但值有多个）的数据。   接收数组变量 ，如checkbox类型     </p></li>
<li><p><b>getParameterNames():</b>该方法可以取得所有变量的名称，该方法返回一个Emumeration。</p></li>
<li><p><b>getInputStream():</b>调用此方法来读取来自客户端的二进制数据流。</p></li>
</ul>
<hr>
<h2>使用URL的 GET 方法实例</h2>

<p>以下是一个简单的URL,并使用GET方法来传递URL中的参数：</p>
<pre>http://localhost:8080/testjsp/main.jsp?name=菜鸟教程&amp;url=http://ww.runoob.com</pre>
<p>testjsp 为项目地址。</p>
<p>以下是 main.jsp 文件的JSP程序用于处理客户端提交的表单数据，我们使用getParameter()方法来获取提交的数据：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;使用 GET 方法读取数据&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;站点名:&lt;/b&gt;
   &lt;%= request.getParameter("name")%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;网址:&lt;/b&gt;
   &lt;%= request.getParameter("url")%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>接下来我们通过浏览器访问 <strong><em>http://localhost:8080/testjsp/main.jsp?name=菜鸟教程&url=http://ww.runoob.com</em></strong>
输出结果如下所示：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/D47A8578-B8F3-4C46-A58E-98EDFF012158.jpg" width="70%"></p>
<hr>
<h2>使用表单的 GET 方法实例</h2>
<p>以下是一个简单的 HTML 表单，该表单通过GET方法将客户端数据提交
到 <strong>main.jsp</strong> 文件中：</p>
<pre>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form action="main.jsp" method="GET"&gt;
站点名: &lt;input type="text" name="name"&gt;
&lt;br /&gt;
网址: &lt;input type="text" name="url" /&gt;
&lt;input type="submit" value="提交" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>将以上HTML代码保存到test.htm文件中。
将该文件放置于当前jsp项目的 WebContent 目录下（与 main.jsp 同一个目录）。</p><p>
通过访问 <strong><em>http://localhost:8080/testjsp/test.html</em></strong> 提交表单数据到 main.jsp 文件，演示 Gif 图如下所示：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp2.gif" ></p>
<p>
在 "站点名" 与 "网址" 两个表单中填入信息，并点击 "提交" 按钮，它将输出结果。</p>

<hr>
<h2>使用表单的 POST 方法实例</h2>
<p>接下来让我们使用POST方法来传递表单数据，修改main.jsp与Hello.htm文件代码，如下所示：</p>

<p>main.jsp文件代码：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;使用 POST 方法读取数据&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;站点名:&lt;/b&gt;
&lt;%
// 解决中文乱码的问题
String name = new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8");
%&gt;
   &lt;%=name%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;网址:&lt;/b&gt;
   &lt;%= request.getParameter("url")%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>代码中我们使用 <strong>new String((request.getParameter("name")).getBytes("ISO-8859-1"),"UTF-8")</strong>来转换编码，防止中文乱码的发生。</p>
<p>以下是test.htm修改后的代码：</p>
<pre>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form action="main.jsp" method="POST"&gt;
站点名: &lt;input type="text" name="name"&gt;
&lt;br /&gt;
网址: &lt;input type="text" name="url" /&gt;
&lt;input type="submit" value="提交" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
通过访问 <strong><em>http://localhost:8080/testjsp/test.html</em></strong> 提交表单数据到 main.jsp 文件，演示 Gif 图如下所示：</p>

<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp3.gif"></p>
<hr>
<h2>传递 Checkbox 数据到JSP程序</h2>
<p>复选框 checkbox 可以传递一个甚至多个数据。</p>
<p>以下是一个简单的HTML代码，并将代码保存在test.htm文件中：</p>
<pre>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form action="main.jsp" method="POST" target="_blank"&gt;
&lt;input type="checkbox" name="google" checked="checked" /&gt; Google
&lt;input type="checkbox" name="runoob"  /&gt; 菜鸟教程
&lt;input type="checkbox" name="taobao" checked="checked" /&gt; 
                                                淘宝
&lt;input type="submit" value="选择网站" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>以上代码在浏览器访问如下所示：</p>
<p>以下为main.jsp文件代码，用于处理复选框数据：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;从复选框中读取数据&lt;/h1&gt;
&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;Google 是否选中:&lt;/b&gt;
   &lt;%= request.getParameter("google")%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;菜鸟教程是否选中:&lt;/b&gt;
   &lt;%= request.getParameter("runoob")%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;淘宝是否选中:&lt;/b&gt;
   &lt;%= request.getParameter("taobao")%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
通过访问 <strong><em>http://localhost:8080/testjsp/test.html</em></strong> 提交表单数据到 main.jsp 文件，演示 Gif 图如下所示：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp4.gif"></p>
<hr>
<h2>读取所有表单参数</h2>
<p>以下我们将使用 <strong>HttpServletRequest</strong> 的 <strong>getParameterNames()</strong> 来读取所有表单参数,该方法可以取得所有变量的名称，该方法返回一个枚举。</p>
<p>一旦我们有了一个 Enumeration（枚举），我们就可以调用 hasMoreElements() 方法来确定是否还有元素，以及使用nextElement（）方法来获得每个参数的名称。</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;读取所有表单参数&lt;/h1&gt;
&lt;table width="100%" border="1" align="center"&gt;
&lt;tr bgcolor="#949494"&gt;
&lt;th&gt;参数名&lt;/th&gt;&lt;th&gt;参数值&lt;/th&gt;
&lt;/tr&gt;
&lt;%
   Enumeration paramNames = request.getParameterNames();

   while(paramNames.hasMoreElements()) {
      String paramName = (String)paramNames.nextElement();
      out.print("&lt;tr&gt;&lt;td&gt;" + paramName + "&lt;/td&gt;\n");
      String paramValue = request.getParameter(paramName);
      out.println("&lt;td&gt; " + paramValue + "&lt;/td&gt;&lt;/tr&gt;\n");
   }
%&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>以下是test.htm文件的内容:</p>
<pre>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form action="main.jsp" method="POST" target="_blank"&gt;
&lt;input type="checkbox" name="google" checked="checked" /&gt; Google
&lt;input type="checkbox" name="runoob"  /&gt; 菜鸟教程
&lt;input type="checkbox" name="taobao" checked="checked" /&gt; 
                                                淘宝
&lt;input type="submit" value="选择网站" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>现在我们通过浏览器访问 test.htm 文件提交数据，输出结果如下：</p>
<p>
通过访问 <strong><em>http://localhost:8080/testjsp/test.html</em></strong> 提交表单数据到 main.jsp 文件，演示 Gif 图如下所示：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp5.gif"></p>
<p>你可以尝试使用以上的JSP代码读取其它对象，如文本框，单选按钮或下拉框等等其他形式的数据。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-http-status-codes.html" rel="prev"> JSP HTTP 状态码</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-writing-filters.html" rel="next"> JSP 过滤器</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-form-processing.html" rel="prev"> JSP 表单处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-cookies.html" rel="next"> JSP Cookie 处理</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">过滤器</span></h1>
<p>JSP 和 Servlet 中的过滤器都是 Java 类。</p>
<p>过滤器可以动态地拦截请求和响应，以变换或使用包含在请求或响应中的信息。</p>
<p>可以将一个或多个过滤器附加到一个 Servlet 或一组 Servlet。过滤器也可以附加到 JavaServer Pages (JSP) 文件和 HTML 页面。</p>
<p>过滤器是可用于 Servlet 编程的 Java 类，可以实现以下目的：</p>
<ul class="list">
<li>在客户端的请求访问后端资源之前，拦截这些请求。</li>
<li>在服务器的响应发送回客户端之前，处理这些响应。</li>
</ul>
<p>根据规范建议的各种类型的过滤器：</p>
<ul class="list">
<li>身份验证过滤器（Authentication Filters）。</li>
<li>数据压缩过滤器（Data compression Filters）。</li>
<li>加密过滤器（Encryption Filters）。</li>
<li>触发资源访问事件过滤器。</li>
<li>图像转换过滤器（Image Conversion Filters）。</li>
<li>日志记录和审核过滤器（Logging and Auditing Filters）。</li> 
<li>MIME-TYPE 链过滤器（MIME-TYPE Chain Filters）。</li>
<li>标记化过滤器（Tokenizing Filters）。</li>
<li>XSL/T 过滤器（XSL/T Filters），转换 XML 内容。</li> 
</ul>
<p>过滤器通过 Web 部署描述符（web.xml）中的 XML 标签来声明，然后映射到您的应用程序的部署描述符中的 Servlet 名称或 URL 模式。</p>
<p>当 Web 容器启动 Web 应用程序时，它会为您在部署描述符中声明的每一个过滤器创建一个实例。</p>
<p>Filter的执行顺序与在web.xml配置文件中的配置顺序一致，一般把Filter配置在所有的Servlet之前。</p>

<h2 class="tutheader">Servlet 过滤器方法</h2>
<p>过滤器是一个实现了 javax.servlet.Filter 接口的 Java 类。javax.servlet.Filter 接口定义了三个方法：</p>
<table class="reference notranslate">
<tr><th style="width:5%">序号</th><th>方法 &amp; 描述</th></tr>
<tr><td>1</td><td><b>public void doFilter (ServletRequest, ServletResponse, FilterChain)</b><br />该方法完成实际的过滤操作，当客户端请求方法与过滤器设置匹配的URL时，Servlet容器将先调用过滤器的doFilter方法。FilterChain用户访问后续过滤器。</td></tr>
<tr><td>2</td><td><b>public void init(FilterConfig filterConfig)</b><br />web 应用程序启动时，web 服务器将创建Filter 的实例对象，并调用其init方法，读取web.xml配置，完成对象的初始化功能，从而为后续的用户请求作好拦截的准备工作（filter对象只会创建一次，init方法也只会执行一次）。开发人员通过init方法的参数，可获得代表当前filter配置信息的FilterConfig对象。</td></tr>
<tr><td>3</td><td><b>public void destroy()</b><br />Servlet容器在销毁过滤器实例前调用该方法，在该方法中释放Servlet过滤器占用的资源。</td></tr>
</table>

<h3>FilterConfig 使用</h3>
<p>Filter 的 init 方法中提供了一个 FilterConfig 对象。</p>
<p>如 web.xml 文件配置如下：</p>
<pre>
&lt;filter&gt;
	&lt;filter-name&gt;LoginFilter&lt;/filter-name&gt;
	&lt;filter-class&gt;com.runoob.test.LogFilter&lt;/filter-class&gt;
	&lt;init-param&gt;
		&lt;param-name&gt;Site&lt;/param-name&gt;
		&lt;param-value&gt;菜鸟教程&lt;/param-value&gt;
	&lt;/init-param&gt;
	&lt;/filter&gt;
</pre>
<p>
<p>在 init 方法使用 FilterConfig 对象获取参数：</p>
<pre>
public void  init(FilterConfig config) throws ServletException {
	// 获取初始化参数
	String site = config.getInitParameter("Site"); 
	// 输出初始化参数
	System.out.println("网站名称: " + site); 
}
</pre>

<h2 class="tutheader">JSP 过滤器实例</h2>
<p>以下是 Servlet 过滤器的实例，将输出网站名称和地址。本实例让您对 Servlet 过滤器有基本的了解，您可以使用相同的概念编写更复杂的过滤器应用程序：</p>
<pre>
//导入必需的 java 库
import javax.servlet.*;
import java.util.*;

//实现 Filter 类
public class LogFilter implements Filter  {
	public void  init(FilterConfig config) throws ServletException {
		// 获取初始化参数
		String site = config.getInitParameter("Site"); 

		// 输出初始化参数
		System.out.println("网站名称: " + site); 
	}
	public void  doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException, ServletException {

		// 输出站点名称
		System.out.println("站点网址：http://www.runoob.com");

		// 把请求传回过滤链
		chain.doFilter(request,response);
	}
	public void destroy( ){
		/* 在 Filter 实例被 Web 容器从服务移除之前调用 */
	}
}
</pre>
<p>DisplayHeader.java  文件代码如下：</p>
<pre>
//导入必需的 java 库
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/DisplayHeader")

//扩展 HttpServlet 类
public class DisplayHeader extends HttpServlet {

	// 处理 GET 方法请求的方法
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		// 设置响应内容类型
		response.setContentType("text/html;charset=UTF-8");

		PrintWriter out = response.getWriter();
		String title = "HTTP Header 请求实例 - 菜鸟教程实例";
		String docType =
			"&lt;!DOCTYPE html&gt; \n";
			out.println(docType +
			"&lt;html&gt;\n" +
			"&lt;head&gt;&lt;meta charset=\"utf-8\"&gt;&lt;title&gt;" + title + "&lt;/title&gt;&lt;/head&gt;\n"+
			"&lt;body bgcolor=\"#f0f0f0\"&gt;\n" +
			"&lt;h1 align=\"center\"&gt;" + title + "&lt;/h1&gt;\n" +
			"&lt;table width=\"100%\" border=\"1\" align=\"center\"&gt;\n" +
			"&lt;tr bgcolor=\"#949494\"&gt;\n" +
			"&lt;th&gt;Header 名称&lt;/th&gt;&lt;th&gt;Header 值&lt;/th&gt;\n"+
			"&lt;/tr&gt;\n");

		Enumeration headerNames = request.getHeaderNames();

		while(headerNames.hasMoreElements()) {
			String paramName = (String)headerNames.nextElement();
			out.print("&lt;tr&gt;&lt;td&gt;" + paramName + "&lt;/td&gt;\n");
			String paramValue = request.getHeader(paramName);
			out.println("&lt;td&gt; " + paramValue + "&lt;/td&gt;&lt;/tr&gt;\n");
		}
		out.println("&lt;/table&gt;\n&lt;/body&gt;&lt;/html&gt;");
	}
	// 处理 POST 方法请求的方法
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
</pre> 

<h3>Web.xml 中的 Servlet 过滤器映射（Servlet Filter Mapping）</h3>
<p>定义过滤器，然后映射到一个 URL 或 Servlet，这与定义 Servlet，然后映射到一个 URL 模式方式大致相同。在部署描述符文件 <b>web.xml</b> 中为 filter 标签创建下面的条目：</p>
<pre>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app&gt;  
&lt;filter&gt;
  &lt;filter-name&gt;LogFilter&lt;/filter-name&gt;
  &lt;filter-class&gt;com.runoob.test.LogFilter&lt;/filter-class&gt;
  &lt;init-param&gt;
    &lt;param-name&gt;Site&lt;/param-name&gt;
    &lt;param-value&gt;菜鸟教程&lt;/param-value&gt;
  &lt;/init-param&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
  &lt;filter-name&gt;LogFilter&lt;/filter-name&gt;
  &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
&lt;servlet&gt;  
  &lt;!-- 类名 --&gt;  
  &lt;servlet-name&gt;DisplayHeader&lt;/servlet-name&gt;  
  &lt;!-- 所在的包 --&gt;  
  &lt;servlet-class&gt;com.runoob.test.DisplayHeader&lt;/servlet-class&gt;  
&lt;/servlet&gt;  
&lt;servlet-mapping&gt;  
  &lt;servlet-name&gt;DisplayHeader&lt;/servlet-name&gt;  
  &lt;!-- 访问的网址 --&gt;  
  &lt;url-pattern&gt;/TomcatTest/DisplayHeader&lt;/url-pattern&gt;  
&lt;/servlet-mapping&gt;  
&lt;/web-app&gt;  
</pre>
<p>上述过滤器适用于所有的 Servlet，因为我们在配置中指定 <b>/*</b> 。如果您只想在少数的 Servlet 上应用过滤器，您可以指定一个特定的 Servlet 路径。</p>
<p>现在试着以常用的方式调用任何 Servlet，您将会看到在 Web 服务器中生成的日志。您也可以使用 Log4J 记录器来把上面的日志记录到一个单独的文件中。</p>
<p>接下来我们访问这个实例地址 <strong>http://localhost:8080/TomcatTest/DisplayHeader</strong>, 然后在控制台看下输出内容，如下所示：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/07/sfilter.jpg"></p>
<h2 class="tutheader">使用多个过滤器</h2>
<p>Web 应用程序可以根据特定的目的定义若干个不同的过滤器。假设您定义了两个过滤器 <i>AuthenFilter</i> 和 <i>LogFilter</i>。您需要创建一个如下所述的不同的映射，其余的处理与上述所讲解的大致相同：</p>
<pre class="prettyprint">
&lt;filter&gt;
   &lt;filter-name&gt;LogFilter&lt;/filter-name&gt;
   &lt;filter-class&gt;com.runoob.test.LogFilter&lt;/filter-class&gt;
   &lt;init-param&gt;
	  &lt;param-name&gt;test-param&lt;/param-name&gt;
	  &lt;param-value&gt;Initialization Paramter&lt;/param-value&gt;
   &lt;/init-param&gt;
&lt;/filter&gt;

&lt;filter&gt;
   &lt;filter-name&gt;AuthenFilter&lt;/filter-name&gt;
   &lt;filter-class&gt;com.runoob.test.AuthenFilter&lt;/filter-class&gt;
   &lt;init-param&gt;
	  &lt;param-name&gt;test-param&lt;/param-name&gt;
	  &lt;param-value&gt;Initialization Paramter&lt;/param-value&gt;
   &lt;/init-param&gt;
&lt;/filter&gt;

&lt;filter-mapping&gt;
   &lt;filter-name&gt;LogFilter&lt;/filter-name&gt;
   &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;

&lt;filter-mapping&gt;
   &lt;filter-name&gt;AuthenFilter&lt;/filter-name&gt;
   &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre>

<h2 class="tutheader">过滤器的应用顺序</h2>
<p>web.xml 中的 filter-mapping 元素的顺序决定了 Web 容器应用过滤器到 Servlet 的顺序。若要反转过滤器的顺序，您只需要在 web.xml 文件中反转 filter-mapping 元素即可。</p>
<p>例如，上面的实例将先应用 LogFilter，然后再应用 AuthenFilter，但是下面的实例将颠倒这个顺序：</p>
<pre>
&lt;filter-mapping&gt;
   &lt;filter-name&gt;AuthenFilter&lt;/filter-name&gt;
   &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;

&lt;filter-mapping&gt;
   &lt;filter-name&gt;LogFilter&lt;/filter-name&gt;
   &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre>
<hr>
<h2>web.xml配置各节点说明</h2>
<ul>
<li><code>&lt;filter&gt;</code>指定一个过滤器。

<ul>
<li><code>&lt;filter-name&gt;</code>用于为过滤器指定一个名字，该元素的内容不能为空。</li>
<li><code>&lt;filter-class&gt;</code>元素用于指定过滤器的完整的限定类名。</li>
<li><code>&lt;init-param&gt;</code>元素用于为过滤器指定初始化参数，它的子元素<code>&lt;param-name&gt;</code>指定参数的名字，<code>&lt;param-value&gt;</code>指定参数的值。</li>
<li>在过滤器中，可以使用<code>FilterConfig</code>接口对象来访问初始化参数。</li>
</ul>
</li>
<li><code>&lt;filter-mapping&gt;</code>元素用于设置一个 Filter 所负责拦截的资源。一个Filter拦截的资源可通过两种方式来指定：Servlet 名称和资源访问的请求路径

<ul>
<li><code>&lt;filter-name&gt;</code>子元素用于设置filter的注册名称。该值必须是在<code>&lt;filter&gt;</code>元素中声明过的过滤器的名字</li>
<li><code>&lt;url-pattern&gt;</code>设置 filter 所拦截的请求路径(过滤器关联的URL样式)</li>
</ul>
</li>
<li><code>&lt;servlet-name&gt;</code>指定过滤器所拦截的Servlet名称。</li>
<li><code>&lt;dispatcher&gt;</code>指定过滤器所拦截的资源被 Servlet 容器调用的方式，可以是<code>REQUEST</code>,<code>INCLUDE</code>,<code>FORWARD</code>和<code>ERROR</code>之一，默认<code>REQUEST</code>。用户可以设置多个<code>&lt;dispatcher&gt;</code>子元素用来指定 Filter 对资源的多种调用方式进行拦截。</li>
<li><code>&lt;dispatcher&gt;</code>子元素可以设置的值及其意义

<ul>
<li><code>REQUEST</code>：当用户直接访问页面时，Web容器将会调用过滤器。如果目标资源是通过RequestDispatcher的include()或forward()方法访问时，那么该过滤器就不会被调用。</li>
<li><code>INCLUDE</code>：如果目标资源是通过RequestDispatcher的include()方法访问时，那么该过滤器将被调用。除此之外，该过滤器不会被调用。</li>
<li><code>FORWARD</code>：如果目标资源是通过RequestDispatcher的forward()方法访问时，那么该过滤器将被调用，除此之外，该过滤器不会被调用。</li>
<li><code>ERROR</code>：如果目标资源是通过声明式异常处理机制调用时，那么该过滤器将被调用。除此之外，过滤器不会被调用。</li>
</ul>
</li>
</ul>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-form-processing.html" rel="prev"> JSP 表单处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-cookies.html" rel="next"> JSP Cookie 处理</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-writing-filters.html" rel="prev"> JSP 过滤器</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-session.html" rel="next"> JSP Session</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">Cookie 处理</span></h1>
<p>Cookie是存储在客户机的文本文件，它们保存了大量轨迹信息。在servlet技术基础上，JSP显然能够提供对HTTP cookie的支持。</p>
<p>
通常有三个步骤来识别回头客：</p>
<ul>
	<li>
		服务器脚本发送一系列cookie至浏览器。比如名字，年龄，ID号码等等。</li>
	<li>
		浏览器在本地机中存储这些信息，以备不时之需。</li>
	<li>
		当下一次浏览器发送任何请求至服务器时，它会同时将这些cookie信息发送给服务器，然后服务器使用这些信息来识别用户或者干些其它事情。</li>
</ul><p>
本章节将会传授您如何去设置或重设cookie的方法，还有如何访问它们及如何删除它们。</p>


<blockquote>
<p>JSP Cookie 处理需要对中文进行编码与解码，方法如下：</p>
<pre>
String   str   =   java.net.URLEncoder.encode("中文");            //编码
String   str   =   java.net.URLDecoder.decode("编码后的字符串");   // 解码
</pre>
</blockquote>



<hr>
<h2>
	Cookie 剖析</h2><p>
Cookie通常在HTTP信息头中设置（虽然JavaScript能够直接在浏览器中设置cookie）。在JSP中，设置一个cookie需要发送如下的信息头给服务器：
<pre>
HTTP/1.1 200 OK
Date: Fri, 04 Feb 2015 21:03:38 GMT
Server: Apache/1.3.9 (UNIX) PHP/4.0b3
Set-Cookie: name=runoob; expires=Friday, 04-Feb-07 22:03:38 GMT; 
                 path=/; domain=runoob.com
Connection: close
Content-Type: text/html
</pre>
<p>
正如您所见，Set-Cookie信息头包含一个键值对，一个GMT（格林尼治标准）时间，一个路径，一个域名。键值对会被编码为URL。有效期域是个指令，告诉浏览器在什么时候之后就可以清除这个cookie。</p>
<p>
如果浏览器被配置成可存储cookie，那么它将会保存这些信息直到过期。如果用户访问的任何页面匹配了cookie中的路径和域名，那么浏览器将会重新将这个cookie发回给服务器。浏览器端的信息头长得就像下面这样：
<pre>
GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.uk:1126
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz
</pre>
<p>
JSP脚本通过request对象中的getCookies()方法来访问这些cookie，这个方法会返回一个Cookie对象的数组。</p><hr>
<h2>
	Servlet Cookie 方法</h2><p>
下表列出了Cookie对象中常用的方法：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>序号</strong></th>
			<th>
				<strong>方法</strong><strong> &amp; </strong><strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				1</td>
			<td>
				<strong>public void setDomain(String pattern)</strong></p>
				设置cookie的域名，比如w3cschool.cc</td>
		</tr>
		<tr>
			<td>
				2</td>
			<td>
				<strong>public String getDomain()</strong></p>
				获取cookie的域名，比如w3cschool.cc</td>
		</tr>
		<tr>
			<td>
				3</td>
			<td>
				<strong>public void setMaxAge(int expiry)</strong></p>
				设置cookie有效期，以秒为单位，默认有效期为当前session的存活时间</td>
		</tr>
		<tr>
			<td>
				4</td>
			<td>
				<strong>public int getMaxAge()</strong></p>
				获取cookie有效期，以秒为单位，默认为-1 ，表明cookie会活到浏览器关闭为止</td>
		</tr>
		<tr>
			<td>
				5</td>
			<td>
				<strong>public String getName()</strong></p>
				返回 cookie的名称，名称创建后将不能被修改</td>
		</tr>
		<tr>
			<td>
				6</td>
			<td>
				<strong>public void setValue(String newValue)</strong></p>
				设置 cookie的值</td>
		</tr>
		<tr>
			<td>
				7</td>
			<td>
				<strong>public String getValue()</strong></p>
				获取cookie的值</td>
		</tr>
		<tr>
			<td>
				8</td>
			<td>
				<strong>public void setPath(String uri)</strong></p>
				设置cookie 的路径，默认为当前页面目录下的所有URL，还有此目录下的所有子目录</td>
		</tr>
		<tr>
			<td>
				9</td>
			<td>
				<strong>public String getPath()</strong></p>
				获取cookie 的路径</td>
		</tr>
		<tr>
			<td>
				10</td>
			<td>
				<strong>public void setSecure(boolean flag)</strong></p>
				指明cookie是否要加密传输</td>
		</tr>
		<tr>
			<td>
				11</td>
			<td>
				<strong>public void setComment(String purpose)</strong></p>
				设置注释描述 cookie的目的。当浏览器将cookie展现给用户时，注释将会变得非常有用</td>
		</tr>
		<tr>
			<td>
				12</td>
			<td>
				<strong>public String getComment()</strong></p>
				返回描述cookie目的的注释，若没有则返回null</td>
		</tr>
	</tbody>
</table><hr>
<h2>使用JSP设置Cookie</h2><p>
使用JSP设置cookie包含三个步骤：</p>
<p><strong>
(1)创建一个Cookie对象： </strong>调用Cookie的构造函数，使用一个cookie名称和值做参数，它们都是字符串。
</p>
<pre>
Cookie cookie = new Cookie("key","value");
</pre><p>
请务必牢记，名称和值中都不能包含空格或者如下的字符：</p>
<pre>
[ ] ( ) = , " / ? @ : ;
</pre>
<p><strong>
(2) 设置有效期：</strong>调用setMaxAge()函数表明cookie在多长时间（以秒为单位）内有效。下面的操作将有效期设为了24小时。</p>
<pre>
cookie.setMaxAge(60*60*24); 
</pre>
<p><strong>
(3) 将cookie发送至HTTP响应头中：</strong>调用response.addCookie()函数来向HTTP响应头中添加cookie。</p>
<pre>
response.addCookie(cookie);
</pre><hr>
<h3>实例演示</h3>
<p>main.jsp 文件代码如下所示：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.net.*" %&gt;
&lt;%
   // 编码，解决中文乱码   
   String str = URLEncoder.encode(request.getParameter("name"),"utf-8");  
   // 设置 name 和 url cookie 
   Cookie name = new Cookie("name",
		   str);
   Cookie url = new Cookie("url",
			  request.getParameter("url"));

   // 设置cookie过期时间为24小时。
   name.setMaxAge(60*60*24); 
   url.setMaxAge(60*60*24); 

   // 在响应头部添加cookie
   response.addCookie( name );
   response.addCookie( url );
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;设置 Cookie&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;设置 Cookie&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;网站名:&lt;/b&gt;
   &lt;%= request.getParameter("name")%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;b&gt;网址:&lt;/b&gt;
   &lt;%= request.getParameter("url")%&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>以下是一个简单的 HTML 表单通过GET方法将客户端数据提交到 main.jsp 文件中,并设置 cookie：</p>
<pre>
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;菜鸟教程(runoob.com)&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;form action="main.jsp" method=GET&gt;
站点名: &lt;input type="text" name="name"&gt;
&lt;br /&gt;
网址: &lt;input type="text" name="url" /&gt;
&lt;input type="submit" value="提交" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>将以上HTML代码保存到test.htm文件中。 </p><p>将该文件放置于当前jsp项目的 WebContent 目录下（与 main.jsp 同一个目录）。</p><p>
通过访问 http://localhost:8080/testjsp/test.html 提交表单数据到 main.jsp 文件，演示 Gif 图如下所示：</p><p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp6.gif">
</p><p>
试着输入 "站点名" 和 "网址"，然后点击提交按钮，它将会在您的屏幕中显示 "站点名" 和 "网址"，并且设置 "站点名" 和 "网址" 的两个 cookie。</p>
<hr>
<h2>使用 JSP 读取 Cookie</h2><p>
想要读取cookie，您就需要调用request.getCookies()方法来获得一个javax.servlet.http.Cookie对象的数组，然后遍历这个数组，使用getName()方法和getValue()方法来获取每一个cookie的名称和值。</p>
<h3实例演示</h3><p>
让我们来读取上个例子中的cookie, 以下为 cookie.jsp 文件代码：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.net.*" %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;获取 Cookie&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
   Cookie cookie = null;
   Cookie[] cookies = null;
   // 获取cookies的数据,是一个数组
   cookies = request.getCookies();
   if( cookies != null ){
      out.println("&lt;h2&gt; 查找 Cookie 名与值&lt;/h2&gt;");
      for (int i = 0; i &lt; cookies.length; i++){
         cookie = cookies[i];
        
         out.print("参数名 : " + cookie.getName());
         out.print("&lt;br&gt;");
         out.print("参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" &lt;br&gt;");
         out.print("------------------------------------&lt;br&gt;");
      }
  }else{
      out.println("&lt;h2&gt;没有发现 Cookie&lt;/h2&gt;");
  }
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
浏览器访问后，输出结果为：
</p>
<p><img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/C6A7341F-029A-4244-8B38-BE010E391091.jpg"></p>
<hr>
<h2>
	使用JSP删除Cookie</h2><p>
删除cookie非常简单。如果您想要删除一个cookie，按照下面给的步骤来做就行了：</p>
<ul>
	<li>
		获取一个已经存在的cookie然后存储在Cookie对象中。</li>
	<li>
		将cookie的有效期设置为0。</li>
	<li>
		将这个cookie重新添加进响应头中。</li>
</ul><hr>
<h3>实例演示</h3><p>
下面的程序删除一个名为"name"的cookie，当您第二次运行cookie.jsp时，name 将会为 null。</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.net.*" %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;获取 Cookie&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
   Cookie cookie = null;
   Cookie[] cookies = null;
   // 获取当前域名下的cookies，是一个数组
   cookies = request.getCookies();
   if( cookies != null ){
	  out.println("&lt;h2&gt; 查找 Cookie 名与值&lt;/h2&gt;");
      for (int i = 0; i &lt; cookies.length; i++){
         cookie = cookies[i];
         if((cookie.getName( )).compareTo("name") == 0 ){
            cookie.setMaxAge(0);
            response.addCookie(cookie);
            out.print("删除 Cookie: " + 
            cookie.getName( ) + "&lt;br/&gt;");
         }
         out.print("参数名 : " + cookie.getName());
         out.print("&lt;br&gt;");
         out.print("参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" &lt;br&gt;");
         out.print("------------------------------------&lt;br&gt;");
      }
  }else{
	  out.println("&lt;h2&gt;没有发现 Cookie&lt;/h2&gt;");
  }
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
通过浏览器访问，输出结果为：</p>
</p>
<p><img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/C6A7341F-029A-4244-8B38-BE010E391091.jpg"></p><p>
再次访问 <strong>http://localhost:8080/testjsp/cookie.jsp</strong>，将会得到如下结果：</p>
<p>
</p>
<p><img  width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/7BF1C669-F7A4-4245-B7B0-B79BBB272604.jpg"></p>
<p>可以看到名为 width="70%" "name" 的 cookie 已经不见了。</p>
<p>
您也可以手动在浏览器中删除 cookie。IE 浏览器通过点击Tools菜单项，然后选择Internet Options，点击 Delete Cookies，就能删除所有 cookie 。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-writing-filters.html" rel="prev"> JSP 过滤器</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-session.html" rel="next"> JSP Session</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-cookies.html" rel="prev"> JSP Cookie 处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-file-uploading.html" rel="next"> JSP 文件上传</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">Session</span></h1>
<p>HTTP是无状态协议，这意味着每次客户端检索网页时，都要单独打开一个服务器连接，因此服务器不会记录下先前客户端请求的任何信息。</p>
<p>
有三种方法来维持客户端与服务器的会话：</p><hr>
<h2>
	Cookies</h2><p>
网络服务器可以指定一个唯一的session ID作为cookie来代表每个客户端，用来识别这个客户端接下来的请求。</p>
<p>
这可能不是一种有效的方式，因为很多时候浏览器并不一定支持cookie，所以我们不建议使用这种方法来维持会话。</p><hr>
<h2>
	隐藏表单域</h2><p>
一个网络服务器可以发送一个隐藏的HTML表单域和一个唯一的session ID，就像下面这样：
<pre>
&lt;input type="hidden" name="sessionid" value="12345"&gt;
</pre>
<p>
这个条目意味着，当表单被提交时，指定的名称和值将会自动包含在GET或POST数据中。每当浏览器发送一个请求，session_id的值就可以用来保存不同浏览器的轨迹。</p>
<p>
这种方式可能是一种有效的方式，但点击&lt;A HREF&gt;标签中的超链接时不会产生表单提交事件，因此隐藏表单域也不支持通用会话跟踪。</p><hr>
<h2>
	重写URL</h2><p>
您可以在每个URL后面添加一些额外的数据来区分会话，服务器能够根据这些数据来关联session标识符。</p>
<p>
举例来说，http://w3cschool.cc/file.htm;sessionid=12345， session标识符为sessionid=12345，服务器可以用这个数据来识别客户端。</p>
<p>
相比而言，重写URL是更好的方式来，就算浏览器不支持cookies也能工作，但缺点是您必须为每个URL动态指定session ID，就算这是个简单的HTML页面。</p>
<hr>
<h2>
	session对象</h2><p>
除了以上几种方法外，JSP利用servlet提供的HttpSession接口来识别一个用户，存储这个用户的所有访问信息。</p>
<p>
默认情况下，JSP允许会话跟踪，一个新的HttpSession对象将会自动地为新的客户端实例化。禁止会话跟踪需要显式地关掉它，通过将page指令中session属性值设为false来实现，就像下面这样：
<pre>
&lt;%@ page session="false" %&gt;
</pre>
<p>
JSP引擎将隐含的session对象暴露给开发者。由于提供了session对象，开发者就可以方便地存储或检索数据。</p>
<p>
下表列出了session对象的一些重要方法：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>S.N.</strong></th>
			<th>
				<strong>方法</strong><strong> &amp; </strong><strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				1</td>
			<td>
				<strong>public Object getAttribute(String name)</strong></p>
				返回session对象中与指定名称绑定的对象，如果不存在则返回null</td>
		</tr>
		<tr>
			<td>
				2</td>
			<td>
				<strong>public Enumeration getAttributeNames()</strong></p>
				返回session对象中所有的对象名称</td>
		</tr>
		<tr>
			<td>
				3</td>
			<td>
				<strong>public long getCreationTime()</strong></p>
				返回session对象被创建的时间， 以毫秒为单位，从1970年1月1号凌晨开始算起</td>
		</tr>
		<tr>
			<td>
				4</td>
			<td>
				<strong>public String getId()</strong></p>
				返回session对象的ID</td>
		</tr>
		<tr>
			<td>
				5</td>
			<td>
				<strong>public long getLastAccessedTime()</strong></p>
				返回客户端最后访问的时间，以毫秒为单位，从1970年1月1号凌晨开始算起</td>
		</tr>
		<tr>
			<td>
				6</td>
			<td>
				<strong>public int getMaxInactiveInterval()</strong></p>
				返回最大时间间隔，以秒为单位，servlet 容器将会在这段时间内保持会话打开</td>
		</tr>
		<tr>
			<td>
				7</td>
			<td>
				<strong>public void invalidate()</strong></p>
				将session无效化，解绑任何与该session绑定的对象</td>
		</tr>
		<tr>
			<td>
				8</td>
			<td>
				<strong>public boolean isNew(</strong></p>
				返回是否为一个新的客户端，或者客户端是否拒绝加入session</td>
		</tr>
		<tr>
			<td>
				9</td>
			<td>
				<strong>public void removeAttribute(String name)</strong></p>
				移除session中指定名称的对象</td>
		</tr>
		<tr>
			<td>
				10</td>
			<td>
				<strong>public void setAttribute(String name, Object value)&nbsp;</strong></p>
				使用指定的名称和值来产生一个对象并绑定到session中</td>
		</tr>
		<tr>
			<td>
				11</td>
			<td>
				<strong>public void setMaxInactiveInterval(int interval)</strong></p>
				用来指定时间，以秒为单位，servlet容器将会在这段时间内保持会话有效</td>
		</tr>
	</tbody>
</table>
<hr>
<h2>JSP Session应用</h2><p>
这个例子描述了如何使用HttpSession对象来获取创建时间和最后一次访问时间。我们将会为request对象关联一个新的session对象，如果这个对象尚未存在的话。</p>

<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;%
   // 获取session创建时间
   Date createTime = new Date(session.getCreationTime());
   // 获取最后访问页面的时间
   Date lastAccessTime = new Date(session.getLastAccessedTime());

   String title = "再次访问菜鸟教程实例";
   Integer visitCount = new Integer(0);
   String visitCountKey = new String("visitCount");
   String userIDKey = new String("userID");
   String userID = new String("ABCD");

   // 检测网页是否由新的访问用户
   if (session.isNew()){
      title = "访问菜鸟教程实例";
      session.setAttribute(userIDKey, userID);
      session.setAttribute(visitCountKey,  visitCount);
   } else {
	   visitCount = (Integer)session.getAttribute(visitCountKey);
	   visitCount += 1;
	   userID = (String)session.getAttribute(userIDKey);
	   session.setAttribute(visitCountKey,  visitCount);
   }
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Session 跟踪&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;Session 跟踪&lt;/h1&gt;

&lt;table border="1" align="center"&gt; 
&lt;tr bgcolor="#949494"&gt;
   &lt;th&gt;Session 信息&lt;/th&gt;
   &lt;th&gt;值&lt;/th&gt;
&lt;/tr&gt; 
&lt;tr&gt;
   &lt;td&gt;id&lt;/td&gt;
   &lt;td&gt;&lt;% out.print( session.getId()); %&gt;&lt;/td&gt;
&lt;/tr&gt; 
&lt;tr&gt;
   &lt;td&gt;创建时间&lt;/td&gt;
   &lt;td&gt;&lt;% out.print(createTime); %&gt;&lt;/td&gt;
&lt;/tr&gt; 
&lt;tr&gt;
   &lt;td&gt;最后访问时间&lt;/td&gt;
   &lt;td&gt;&lt;% out.print(lastAccessTime); %&gt;&lt;/td&gt;
&lt;/tr&gt; 
&lt;tr&gt;
   &lt;td&gt;用户 ID&lt;/td&gt;
   &lt;td&gt;&lt;% out.print(userID); %&gt;&lt;/td&gt;
&lt;/tr&gt; 
&lt;tr&gt;
   &lt;td&gt;访问次数&lt;/td&gt;
   &lt;td&gt;&lt;% out.print(visitCount); %&gt;&lt;/td&gt;
&lt;/tr&gt; 
&lt;/table&gt; 
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
试着访问 <strong>http://localhost:8080/testjsp/main.jsp</strong> ，第一次运行时将会得到如下结果：</p>
<p>
<img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/sessjsp1.jpg" />
</p>
<p>
再次访问，将会得到如下结果：</p>
<p>
<img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/sessjsp2.jpg" />
</p>
<hr>
<h2>删除Session数据</h2><p>
当处理完一个用户的会话数据后，您可以有如下选择：</p>
<ul>
	<li><strong>
		移除一个特定的属性：</strong><p>调用public void removeAttribute(String name)&nbsp; 方法来移除指定的属性。</p></li>
	<li><strong>
		删除整个会话：</strong><p>调用public void invalidate() 方法来使整个session无效。</p></li>
	<li><strong>
		设置会话有效期：</strong><p>调用 public void setMaxInactiveInterval(int interval)&nbsp; 方法来设置session超时。</p></li>
	<li><strong>
		登出用户：</strong><p>支持servlet2.4版本的服务器，可以调用 logout()方法来登出用户，并且使所有相关的session无效。</p></li>
	<li><strong>
		配置web.xml文件：</strong><p>如果使用的是Tomcat，可以向下面这样配置web.xml文件：</p></li>
</ul>

<pre>
  &lt;session-config&gt;
    &lt;session-timeout&gt;15&lt;/session-timeout&gt;
  &lt;/session-config&gt;
</pre>
<p>
超时以分钟为单位，Tomcat中的默认的超时时间是30分钟。</p>
<p>
Servlet中的getMaxInactiveInterval( ) 方法以秒为单位返回超时时间。如果在web.xml中配置的是15分钟，则getMaxInactiveInterval( ) 方法将会返回900。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-cookies.html" rel="prev"> JSP Cookie 处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-file-uploading.html" rel="next"> JSP 文件上传</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-session.html" rel="prev"> JSP Session</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-handling-date.html" rel="next"> JSP 日期处理</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">文件上传</span></h1>


<p>JSP 可以与 HTML form 标签一起使用，来允许用户上传文件到服务器。上传的文件可以是文本文件或图像文件或任何文档。</p>

<p>本章节我们使用 Servlet 来处理文件上传，使用到的文件有：</p>
<ul>
<li>upload.jsp : 文件上传表单。</li>
<li>message.jsp : 上传成功后跳转页面。</li>
<li>UploadServlet.java : 上传处理 Servlet。</li>
<li>需要引入的 jar 文件：commons-fileupload-1.3.2、commons-io-2.5.jar。</li>
</ul>
<p>结构图如下所示：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/07/sup1.jpg" width="50%"></p>
<p>接下来我们详细介绍。</p>
<hr>

<h2>创建一个文件上传表单</h2> 
<p>下面的 HTML 代码创建了一个文件上传表单。以下几点需要注意：</p>
<ul class="list">
<li>表单 <b>method</b> 属性应该设置为 <b>POST</b> 方法，不能使用 GET 方法。</li>
<li>表单 <b>enctype</b> 属性应该设置为 <b>multipart/form-data</b>.</li>
<li>表单 <b>action</b> 属性应该设置为在后端服务器上处理文件上传的 Servlet 文件。下面的实例使用了 <b>UploadServlet</b> Servlet 来上传文件。</li>
<li>上传单个文件，您应该使用单个带有属性 type="file" 的 &lt;input .../&gt; 标签。为了允许多个文件上传，请包含多个 name 属性值不同的 input 标签。输入标签具有不同的名称属性的值。浏览器会为每个 input 标签关联一个浏览按钮。</li>
</ul>
<p>upload.jsp 文件代码如下：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;文件上传实例 - 菜鸟教程&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;文件上传实例 - 菜鸟教程&lt;/h1&gt;
&lt;form method="post" action="/TomcatTest/UploadServlet" enctype="multipart/form-data"&gt;
	选择一个文件:
	&lt;input type="file" name="uploadFile" /&gt;
	&lt;br/&gt;&lt;br/&gt;
	&lt;input type="submit" value="上传" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>


<h2 class="tutheader">编写后台 Servlet</h2>

<p>以下是 UploadServlet 的源代码，同于处理文件上传，在这之前我们先确保依赖包已经引入到项目的 WEB-INF/lib 目录下：</p>
<ul class="list">
<li>下面的实例依赖于 FileUpload，所以一定要确保在您的 classpath 中有最新版本的 <b>commons-fileupload.x.x.jar</b> 文件。可以从 <a href="http://commons.apache.org/proper/commons-fileupload/" target="_blank">http://commons.apache.org/proper/commons-fileupload/</a> 下载。</li>
<li>FileUpload 依赖于 Commons IO，所以一定要确保在您的 classpath 中有最新版本的 <b>commons-io-x.x.jar</b> 文件。可以从 <a href="http://commons.apache.org/proper/commons-io/" target="_blank">http://commons.apache.org/proper/commons-io/</a> 下载。</li>
</ul>

<p>你可以直接下载本站提供的两个依赖包：</p>
<ul>
<li><a href="http://static.runoob.com/download/commons-fileupload-1.3.2.jar" target="_blank" download>commons-fileupload-1.3.2.jar</a></li>
<li><a href="http://static.runoob.com/download/commons-io-2.5.jar" target="_blank" download>commons-io-2.5.jar</a></li>
</ul>
<p>UploadServlet 的源代码 如下所示：</p>
<pre>
package com.runoob.test;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
 

/**
 * Servlet implementation class UploadServlet
 */
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
     
    // 上传文件存储目录
    private static final String UPLOAD_DIRECTORY = "upload";
 
    // 上传配置
    private static final int MEMORY_THRESHOLD   = 1024 * 1024 * 3;  // 3MB
    private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
    private static final int MAX_REQUEST_SIZE   = 1024 * 1024 * 50; // 50MB
 
    /**
     * 上传数据及保存文件
     */
    protected void doPost(HttpServletRequest request,
		HttpServletResponse response) throws ServletException, IOException {
		// 检测是否为多媒体上传
		if (!ServletFileUpload.isMultipartContent(request)) {
		    // 如果不是则停止
		    PrintWriter writer = response.getWriter();
		    writer.println("Error: 表单必须包含 enctype=multipart/form-data");
		    writer.flush();
		    return;
		}
 
        // 配置上传参数
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 设置内存临界值 - 超过后将产生临时文件并存储于临时目录中
        factory.setSizeThreshold(MEMORY_THRESHOLD);
        // 设置临时存储目录
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));
 
        ServletFileUpload upload = new ServletFileUpload(factory);
         
        // 设置最大文件上传值
        upload.setFileSizeMax(MAX_FILE_SIZE);
         
        // 设置最大请求值 (包含文件和表单数据)
        upload.setSizeMax(MAX_REQUEST_SIZE);
 
        // 构造临时路径来存储上传的文件
        // 这个路径相对当前应用的目录
        String uploadPath = getServletContext().getRealPath("./") + File.separator + UPLOAD_DIRECTORY;
       
         
        // 如果目录不存在则创建
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }
 
        try {
            // 解析请求的内容提取文件数据
            @SuppressWarnings("unchecked")
            List&lt;FileItem&gt; formItems = upload.parseRequest(request);
 
            if (formItems != null &amp;&amp; formItems.size() &gt; 0) {
                // 迭代表单数据
                for (FileItem item : formItems) {
                    // 处理不在表单中的字段
                    if (!item.isFormField()) {
                        String fileName = new File(item.getName()).getName();
                        String filePath = uploadPath + File.separator + fileName;
                        File storeFile = new File(filePath);
                        // 在控制台输出文件的上传路径
                        System.out.println(filePath);
                        // 保存文件到硬盘
                        item.write(storeFile);
                        request.setAttribute("message",
                            "文件上传成功!");
                    }
                }
            }
        } catch (Exception ex) {
            request.setAttribute("message",
                    "错误信息: " + ex.getMessage());
        }
        // 跳转到 message.jsp
        getServletContext().getRequestDispatcher("/message.jsp").forward(
                request, response);
    }
}
</pre>
<p>message.jsp 文件代码如下：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
&lt;title&gt;文件上传结果&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;center&gt;
        &lt;h2&gt;${message}&lt;/h2&gt;
    &lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h2 class="tutheader">编译和运行 Servlet</h2>
<p>编译上面的 Servlet UploadServlet，并在 web.xml 文件中创建所需的条目，如下所示：</p>
<pre>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5"&gt;
  &lt;servlet&gt;
    &lt;display-name&gt;UploadServlet&lt;/display-name&gt;
    &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.runoob.test.UploadServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
   
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;UploadServlet&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/TomcatTest/UploadServlet&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
&lt;/web-app&gt;
</pre>
<p>现在尝试使用您在上面创建的 HTML 表单来上传文件。当您在浏览器中访问：http://localhost:8080/TomcatTest/upload.jsp ，演示如下所示：</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2014/07/servlet8.gif"></p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-session.html" rel="prev"> JSP Session</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-handling-date.html" rel="next"> JSP 日期处理</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-file-uploading.html" rel="prev"> JSP 文件上传</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-page-redirect.html" rel="next"> JSP 页面重定向</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">日期处理</span></h1>
<p>使用JSP最重要的优势之一，就是可以使用所有Java &nbsp;API。本章将会详细地讲述Java中的Date类，它在java.util包下，封装了当前日期和时间。</p>
<p>
Date类有两个构造函数。第一个构造函数使用当前日期和时间来初始化对象。</p>
<pre>
Date( )
</pre>
<p>
第二个构造函数接受一个参数，这个参数表示从1970年1月1日凌晨至所要表示时间的毫秒数。</p>
<pre>
Date(long millisec)
</pre>
<p>
获取Date对象后，您就能够使用下表列出的所有方法：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>序号</strong></th>
			<th>
				<strong>方法</strong><strong> &amp; </strong><strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				1</td>
			<td>
				<strong>boolean after(Date date)</strong></p>
				如果比给定的日期晚，则返回true，否则返回false</td>
		</tr>
		<tr>
			<td>
				2</td>
			<td>
				<strong>boolean before(Date date)</strong></p>
				如果比给定的日期早，则返回true，否则返回false</td>
		</tr>
		<tr>
			<td>
				3</td>
			<td>
				<strong>Object clone( )</strong></p>
				获取当前对象的一个副本</td>
		</tr>
		<tr>
			<td>
				4</td>
			<td>
				<strong>int compareTo(Date date)</strong></p>
				如果与给定日期相等，则返回0，如果比给定日期早，则返回一个负数，如果比给定日期晚，则返回一个正数</td>
		</tr>
		<tr>
			<td>
				5</td>
			<td>
				<strong>int compareTo(Object obj)</strong></p>
				与 compareTo(Date) 方法相同，如果 obj 不是Date类或其子类的对象，抛出ClassCastException异常</td>
		</tr>
		<tr>
			<td>
				6</td>
			<td>
				<strong>boolean equals(Object date)</strong></p>
				如果与给定日期相同，则返回true，否则返回false</td>
		</tr>
		<tr>
			<td>
				7</td>
			<td>
				<strong>long getTime( )</strong></p>
				返回从1970年1月1日凌晨至此对象所表示时间的毫秒数</td>
		</tr>
		<tr>
			<td>
				8</td>
			<td>
				<strong>int hashCode( )</strong></p>
				返回此对象的哈希码</td>
		</tr>
		<tr>
			<td>
				9</td>
			<td>
				<strong>void setTime(long time)</strong></p>
				使用给定参数设置时间和日期，参数time表示从1970年1月1日凌晨至time所经过的毫秒数</td>
		</tr>
		<tr>
			<td>
				10</td>
			<td>
				<strong>String toString( )</strong></p>
				将此对象转换为字符串并返回这个字符串</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	获取当前日期和时间</h2><p>
使用JSP编程可以很容易的获取当前日期和时间，只要使用Date对象的toString()方法就行了，就像下面这样：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*, javax.servlet.*" %&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;显示当前时间与日期&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;显示当前时间与日期&lt;/h1&gt;

&lt;%
   Date date = new Date();
   out.print( "&lt;h2 align=\"center\"&gt;" +date.toString()+"&lt;/h2&gt;");
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
将上面的代码保存在 main.jsp 文件中，然后访问 <strong>http://localhost:8080/testjsp/main.jsp</strong>，运行结果如下：
</p>
<pre>
显示当前时间与日期

Sat Jun 25 17:54:34 CST 2016
</pre>
<p>
刷新 <strong>http://localhost:8080/testjsp/main.jsp</strong>，就可以发现每次刷新所得到的秒数都不相同。</p><hr>
<h2>日期比较</h2><p>
就像我在开头所提到的，您可以在JSP脚本中使用任何Java方法。如果您想要比较两个日期，</p><p>
可以参照下面的方法来做：</p>
<ul>
	<li>
		使用getTime()方法得到毫秒数，然后比较毫秒数就行了。</li>
	<li>
		使用before()，after()，equals()方法。比如，new Date(99,2,12).before(new Date(99,2,18))返回true。</li>
	<li>
		使用compareTo()方法，这个方法在Comparable接口中定义，在Date中实现。</li>
</ul><hr>
<h2>
	使用SimpleDateFormat格式化日期</h2><p>
SimpleDateFormat使用一种地区敏感的方式来格式化和解析日期，它允许您使用自定义的模式来格式化日期和时间。</p>
<p>
对CurrentDate.jsp稍作修改，得到如下修改后的代码：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;%@ page import="javax.servlet.*,java.text.*" %&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;显示当前时间与日期&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;显示当前时间与日期&lt;/h1&gt;

&lt;%
   Date dNow = new Date( );
   SimpleDateFormat ft = 
   new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
   out.print( "&lt;h2 align=\"center\"&gt;" + ft.format(dNow) + "&lt;/h2&gt;");
%&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre><p>
再次编译 main.jsp，然后访问 <strong>http://localhost:8080/testjsp/main.jsp</strong>，就可以得到如下结果：
</p>
<pre>
显示当前时间与日期

2016-06-25 17:57:53
</pre><hr>
<h2>SimpleDateFormat格式码</h2><p>
要指定模式字符串，需要使用下表列出的格式码：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>字符</strong></th>
			<th>
				<strong>描述</strong></th>
			<th>
				<strong>示例</strong></th>
		</tr>
		<tr>
			<td>
				G</td>
			<td>
				时代标识符</td>
			<td>
				AD</td>
		</tr>
		<tr>
			<td>
				y</td>
			<td>
				4位数年份</td>
			<td>
				2001</td>
		</tr>
		<tr>
			<td>
				M</td>
			<td>
				月</td>
			<td>
				July or 07</td>
		</tr>
		<tr>
			<td>
				d</td>
			<td>
				日</td>
			<td>
				10</td>
		</tr>
		<tr>
			<td>
				h</td>
			<td>
				12小时制， A.M./P.M. (1~12)</td>
			<td>
				12</td>
		</tr>
		<tr>
			<td>
				H</td>
			<td>
				24小时制</td>
			<td>
				22</td>
		</tr>
		<tr>
			<td>
				m</td>
			<td>
				分钟</td>
			<td>
				30</td>
		</tr>
		<tr>
			<td>
				s</td>
			<td>
				秒</td>
			<td>
				55</td>
		</tr>
		<tr>
			<td>
				S</td>
			<td>
				毫秒</td>
			<td>
				234</td>
		</tr>
		<tr>
			<td>
				E</td>
			<td>
				星期</td>
			<td>
				Tuesday</td>
		</tr>
		<tr>
			<td>
				D</td>
			<td>
				一年中的某天</td>
			<td>
				360</td>
		</tr>
		<tr>
			<td>
				F</td>
			<td>
				一个月中某星期的某天</td>
			<td>
				2 (second Wed. in July)</td>
		</tr>
		<tr>
			<td>
				w</td>
			<td>
				一年中的某星期</td>
			<td>
				40</td>
		</tr>
		<tr>
			<td>
				W</td>
			<td>
				一个月中的某星期</td>
			<td>
				1</td>
		</tr>
		<tr>
			<td>
				a</td>
			<td>
				A.M./P.M. 标记</td>
			<td>
				PM</td>
		</tr>
		<tr>
			<td>
				k</td>
			<td>
				一天中的某个小时 (1~24)</td>
			<td>
				24</td>
		</tr>
		<tr>
			<td>
				K</td>
			<td>
				一天中的某个小时，A.M./P.M. (0~11)</td>
			<td>
				10</td>
		</tr>
		<tr>
			<td>
				z</td>
			<td>
				时区</td>
			<td>
				Eastern Standard Time</td>
		</tr>
		<tr>
			<td>
				'</td>
			<td>
				文本分隔</td>
			<td>
				Delimiter</td>
		</tr>
		<tr>
			<td>
				"</td>
			<td>
				单引号</td>
			<td>
				`</td>
		</tr>
	</tbody>
</table>
<p>
更多关于Date类的详细信息请查阅Java API文档。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-file-uploading.html" rel="prev"> JSP 文件上传</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-page-redirect.html" rel="next"> JSP 页面重定向</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-handling-date.html" rel="prev"> JSP 日期处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-hits-counter.html" rel="next"> JSP 点击量统计</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">页面重定向</span></h1>
<p>当需要将文档移动到一个新的位置时，就需要使用JSP重定向了。</p>
<p>
最简单的重定向方式就是使用response对象的sendRedirect()方法。这个方法的签名如下：</p>
<pre>
public void response.sendRedirect(String location)
throws IOException 
</pre>
<p>

这个方法将状态码和新的页面位置作为响应发回给浏览器。您也可以使用setStatus()和setHeader()方法来得到同样的效果：
</p>
<pre>
....
String site = "http://www.runoob.com" ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site); 
....
</pre>
<hr>
<h2>实例演示</h2>
<p>
这个例子表明了JSP如何进行页面重定向：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;页面重定向&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h1&gt;页面重定向&lt;/h1&gt;

&lt;%
   // 重定向到新地址
   String site = new String("http://www.runoob.com");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 
%&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
将以上代码保存在PageRedirecting.jsp文件中，然后访问http://localhost:8080/PageRedirect.jsp，它将会把您带至<a href="http://www.runoob.cc/" target="_blank">http://www.runoob.cc/</a>。</p>
			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-handling-date.html" rel="prev"> JSP 日期处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-hits-counter.html" rel="next"> JSP 点击量统计</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-page-redirect.html" rel="prev"> JSP 页面重定向</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-auto-refresh.html" rel="next"> JSP 自动刷新</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">点击量统计</span></h1>
<p>有时候我们需要知道某个页面被访问的次数，这时我们就需要在页面上添加页面统计器，页面访问的统计一般在用户第一次载入时累加该页面的访问数上。</p>
<p>要实现一个计数器，您可以利用应用程序隐式对象和相关方法getAttribute()和setAttribute()来实现。</p>
<p>这个对象表示JSP页面的整个生命周期中。当JSP页面初始化时创建此对象，当JSP页面调用jspDestroy()时删除该对象。</p>
<p>以下是在应用中创建变量的语法：</p>
<pre>
application.setAttribute(String Key, Object Value);
</pre>
<p>您可以使用上述方法来设置一个计数器变量及更新该变量的值。读取该变量的方法如下：</p>
<pre>
application.getAttribute(String Key);
</pre>
<p>
在页面每次被访问时，你可以读取计数器的当前值，并递增1，然后重新设置，在下一个用户访问时就将新的值显示在页面上。</p>
<hr>
<h2>实例演示</h2>
<p>该实例将介绍如何使用JSP来计算特定页面访问的总人数。如果你要计算你网站使用页面的总点击量，那么你就必须将该代码放在所有的JSP页面上。</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;访问量统计&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
    Integer hitsCount = 
      (Integer)application.getAttribute("hitCounter");
    if( hitsCount ==null || hitsCount == 0 ){
       /* 第一次访问 */
       out.println("欢迎访问菜鸟教程!");
       hitsCount = 1;
    }else{
       /* 返回访问值 */
       out.println("欢迎再次访问菜鸟教程!");
       hitsCount += 1;
    }
    application.setAttribute("hitCounter", hitsCount);
%&gt;

&lt;p&gt;页面访问量为: &lt;%= hitsCount%&gt;&lt;/p&gt;


&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>现在我们将上面的代码放置于main.jsp文件上，并访问<em>http://localhost:8080/testjsp/main.jsp</em>文件。你会看到页面会生成个计数器，在我们每次刷新页面时，计数器都会发生变化（每次刷新增加1）。</p>
你也可以通过不同的浏览器访问，计数器会在每次访问后增加1。如下所示：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp7.gif"></p>
<hr>
<h2>复位计数器</h2>
<p>使用以上方法，在 web 服务器重启后，计数器会被复位为 0，即前面保留的数据都会消失，你可以使用一下几种方式解决该问题:</p>
<ul>
<li>在数据库中定义一个用于统计网页访问量的数据表 count，字段为 hitcount，hitcount 默认值为0，将统计数据写入到数据表中。</li>
<li>在每次访问时我们读取表中 hitcount 字段。</p></li>
<li>每次访问时让 hitcount 自增 1。</li>
<li>在页面上显示新的 hitcount 值作为页面的访问量。</p></li>
<li>如果你需要统计每个页面的访问量，你可以使用以上逻辑将代码添加到所有页面上。</li>
</ul>
			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-page-redirect.html" rel="prev"> JSP 页面重定向</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-auto-refresh.html" rel="next"> JSP 自动刷新</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-hits-counter.html" rel="prev"> JSP 点击量统计</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-sending-email.html" rel="next"> JSP 发送邮件</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">自动刷新</span></h1>
<p>想象一下，如果要直播比赛的比分，或股票市场的实时状态，或当前的外汇配给，该怎么实现呢？显然，要实现这种实时功能，您就不得不规律性地刷新页面。</p>
<p>
JSP提供了一种机制来使这种工作变得简单，它能够定时地自动刷新页面。</p>
<p>
刷新一个页面最简单的方式就是使用response对象的setIntHeader()方法。这个方法的签名如下：
<pre>
public void setIntHeader(String header, int headerValue)
</pre>
<p>
这个方法通知浏览器在给定的时间后刷新，时间以秒为单位。</p>
<hr>
<h2>
	页面自动刷新程序示例</h2>
	<p>
这个例子使用了setIntHeader()方法来设置刷新头，模拟一个数字时钟：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;自动刷新实例&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h2&gt;自动刷新实&lt;/h2&gt;
&lt;%
   // 设置每隔5秒刷新一次
   response.setIntHeader("Refresh", 5);
   // 获取当前时间
   Calendar calendar = new GregorianCalendar();
   String am_pm;
   int hour = calendar.get(Calendar.HOUR);
   int minute = calendar.get(Calendar.MINUTE);
   int second = calendar.get(Calendar.SECOND);
   if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
   else
      am_pm = "PM";
   String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
   out.println("当前时间为: " + CT + "\n");
%&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>把以上代码保存在main.jsp文件中，访问它。它会每隔5秒钟刷新一次页面并获取系统当前时间。运行结果如下：</p>
<pre>
自动刷新实

当前时间为: 6:5:36 PM
</pre>
<p>您也可以自己动手写个更复杂点的程序。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-hits-counter.html" rel="prev"> JSP 点击量统计</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-sending-email.html" rel="next"> JSP 发送邮件</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-auto-refresh.html" rel="prev"> JSP 自动刷新</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-database-access.html" rel="next"> JSP 连接数据库</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">发送邮件</span></h1>
<p>虽然使用JSP实现邮件发送功能很简单，但是需要有JavaMail API，并且需要安装JavaBean Activation Framework。</p>
<ul class="list">
<li>您可以从 Java 网站下载最新版本的 <a href="http://www.oracle.com/technetwork/java/javamail/index.html" target="_blank">JavaMail</a>，打开网页右侧有个 <strong>Downloads</strong> 链接，点击它下载。</li>
<li>您可以从 Java 网站下载最新版本的 <a href="http://www.oracle.com/technetwork/articles/java/index-135046.html" target="_blank">JAF（版本 1.1.1）</a>。</li>
</ul>
<p>你也可以使用本站提供的下载链接：</p>
<ul>
<li>
<a href="http://static.runoob.com/download/mail.jar" target="_blank" download>JavaMail mail.jar 1.4.5</a>
<li>
<li>
<a href="http://static.runoob.com/download/activation.jar" target="_blank" download> JAF（版本 1.1.1） activation.jar</a>
<li>
</ul><p>
下载并解压这些文件，在根目录下，您将会看到一系列jar包。将mail.jar包和activation.jar包加入CLASSPATH变量中。</p>
<h2>
	发送一封简单的邮件</h2><p>
这个例子展示了如何从您的机器发送一封简单的邮件。它假定localhost已经连接至网络并且有能力发送一封邮件。与此同时，请再一次确认mail.jar包和activation.jar包已经添加进CLASSPATH变量中。</p>
<pre>
&lt;%@ page import="java.io.*,java.util.*,javax.mail.*"%&gt;
&lt;%@ page import="javax.mail.internet.*,javax.activation.*"%&gt;
&lt;%@ page import="javax.servlet.http.*,javax.servlet.*" %&gt;
&lt;%
   String result;
   // 收件人的电子邮件
   String to = "abcd@gmail.com";

   // 发件人的电子邮件
   String from = "mcmohd@gmail.com";

   // 假设你是从本地主机发送电子邮件
   String host = "localhost";

   // 获取系统属性对象
   Properties properties = System.getProperties();

   // 设置邮件服务器
   properties.setProperty("mail.smtp.host", host);

   // 获取默认的Session对象。
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // 创建一个默认的MimeMessage对象。
      MimeMessage message = new MimeMessage(mailSession);
      // 设置 From: 头部的header字段
      message.setFrom(new InternetAddress(from));
      // 设置 To: 头部的header字段
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // 设置 Subject: header字段
      message.setSubject("This is the Subject Line!");
      // 现在设置的实际消息
      message.setText("This is actual message");
      // 发送消息
      Transport.send(message);
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Send Email using JSP&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;Send Email using JSP&lt;/h1&gt;
&lt;/center&gt;
&lt;p align="center"&gt;
&lt;% 
   out.println("Result: " + result + "\n");
%&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
现在访问http://localhost:8080/SendEmail.jsp，它将会发送一封邮件给abcd@gmail.com 并显示如下结果：
</p>
<pre>
Send Email using JSP
Result: Sent message successfully....
</pre>
<p>
如果想要把邮件发送给多人，下面列出的方法可以用来指明多个邮箱地址：
</p>
<pre>
void addRecipients(Message.RecipientType type, 
                   Address[] addresses)
throws MessagingException
</pre>
<p>
参数的描述如下：</p>
<ul>
	<li>
		type：这个值将会被设置成TO，CC,或BCC。CC代表副本，BCC代表黑色副本，例子程序中使用的是TO。</li>
	<li>
		addresses：这是一个邮箱地址的数组，当指定邮箱地址时需要使用InternetAddress()方法。</li>
</ul><hr>
<h2>
	发送一封HTML邮件</h2><p>
这个例子发送一封简单的HTML邮件。它假定您的localhost已经连接至网络并且有能力发送邮件。与此同时，请再一次确认mail.jar包和activation.jar包已经添加进CLASSPATH变量中。</p>
<p>
这个例子和前一个例子非常相似，不过在这个例子中我们使用了setContent()方法，将"text/html"做为第二个参数传给它，用来表明消息中包含了HTML内容。</p>

<pre>
&lt;%@ page import="java.io.*,java.util.*,javax.mail.*"%&gt;
&lt;%@ page import="javax.mail.internet.*,javax.activation.*"%&gt;
&lt;%@ page import="javax.servlet.http.*,javax.servlet.*" %&gt;
&lt;%
   String result;
   // 收件人的电子邮件
   String to = "abcd@gmail.com";

   // 发件人的电子邮件
   String from = "mcmohd@gmail.com";

   // 假设你是从本地主机发送电子邮件
   String host = "localhost";

   // 获取系统属性对象
   Properties properties = System.getProperties();

   // 设置邮件服务器
   properties.setProperty("mail.smtp.host", host);

   // 获取默认的Session对象。
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // 创建一个默认的MimeMessage对象。
      MimeMessage message = new MimeMessage(mailSession);
      // 设置 From: 头部的header字段
      message.setFrom(new InternetAddress(from));
      // 设置 To: 头部的header字段
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));
      // 设置 Subject: header字段
      message.setSubject("This is the Subject Line!");
     
      // 设置 HTML消息
      message.setContent("&lt;h1&gt;This is actual message&lt;/h1&gt;",
                            "text/html" );
      // 发送消息
      Transport.send(message);
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Send HTML Email using JSP&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;Send Email using JSP&lt;/h1&gt;
&lt;/center&gt;
&lt;p align="center"&gt;
&lt;% 
   out.println("Result: " + result + "\n");
%&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>现在你可以尝试使用以上JSP文件来发送HTML消息的电子邮件。</p><hr>
<h2>
	在邮件中包含附件</h2><p>
这个例子告诉我们如何发送一封包含附件的邮件。</p>
<pre>
&lt;%@ page import="java.io.*,java.util.*,javax.mail.*"%&gt;
&lt;%@ page import="javax.mail.internet.*,javax.activation.*"%&gt;
&lt;%@ page import="javax.servlet.http.*,javax.servlet.*" %&gt;
&lt;%
   String result;
   // 收件人的电子邮件
   String to = "abcd@gmail.com";

   // 发件人的电子邮件
   String from = "mcmohd@gmail.com";

   // 假设你是从本地主机发送电子邮件
   String host = "localhost";

   // 获取系统属性对象
   Properties properties = System.getProperties();

   // 设置邮件服务器
   properties.setProperty("mail.smtp.host", host);

   // 获取默认的Session对象。
   Session mailSession = Session.getDefaultInstance(properties);

   try{
      // 创建一个默认的MimeMessage对象。
      MimeMessage message = new MimeMessage(mailSession);

      // 设置 From: 头部的header字段
      message.setFrom(new InternetAddress(from));

      // 设置 To: 头部的header字段
      message.addRecipient(Message.RecipientType.TO,
                               new InternetAddress(to));

      // 设置 Subject: header字段
      message.setSubject("This is the Subject Line!");

      // 创建消息部分
      BodyPart messageBodyPart = new MimeBodyPart();

      // 填充消息
      messageBodyPart.setText("This is message body");
      
      // 创建多媒体消息
      Multipart multipart = new MimeMultipart();

      // 设置文本消息部分
      multipart.addBodyPart(messageBodyPart);

      // 附件部分
      messageBodyPart = new MimeBodyPart();
      String filename = "file.txt";
      DataSource source = new FileDataSource(filename);
      messageBodyPart.setDataHandler(new DataHandler(source));
      messageBodyPart.setFileName(filename);
      multipart.addBodyPart(messageBodyPart);

      // 发送完整消息
      message.setContent(multipart );

      // 发送消息
      Transport.send(message);
      String title = "Send Email";
      result = "Sent message successfully....";
   }catch (MessagingException mex) {
      mex.printStackTrace();
      result = "Error: unable to send message....";
   }
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Send Attachement Email using JSP&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;Send Attachement Email using JSP&lt;/h1&gt;
&lt;/center&gt;
&lt;p align="center"&gt;
&lt;% 
   out.println("Result: " + result + "\n");
%&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<hr>
<h2>
	用户认证部分</h2><p>
如果邮件服务器需要用户名和密码来进行用户认证的话，可以像下面这样来设置：</p>
<pre>
 props.setProperty("mail.user", "myuser");
 props.setProperty("mail.password", "mypwd");
</pre><hr>
<h2>
	使用表单发送邮件</h2><p>
使用HTML表单接收一封邮件，并通过request对象获取所有邮件信息：</p>
<pre>
String to = request.getParameter("to");
String from = request.getParameter("from");
String subject = request.getParameter("subject");
String messageText = request.getParameter("body");
</pre>
<p>
获取以上信息后，您就可以使用前面提到的例子来发送邮件了。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-auto-refresh.html" rel="prev"> JSP 自动刷新</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-database-access.html" rel="next"> JSP 连接数据库</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jstl-function-trim.html" rel="prev"> JSTL fn:trim()函数</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/eclipse-jsp.html" rel="next"> Eclipse JSP/Servlet 环境搭建</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">标准标签库（JSTL）</span></h1>
<p>JSP标准标签库（JSTL）是一个JSP标签集合，它封装了JSP应用的通用核心功能。</p>
<p>
JSTL支持通用的、结构化的任务，比如迭代，条件判断，XML文档操作，国际化标签，SQL标签。 除了这些，它还提供了一个框架来使用集成JSTL的自定义标签。</p>
<p>
根据JSTL标签所提供的功能，可以将其分为5个类别。</p>
<ul>
<li><b>核心标签</b> </li>
<li><b>格式化标签</b></li>
<li><b>SQL 标签</b> </li>
<li><b>XML 标签</b> </li>
<li><b>JSTL 函数</b></li>
</ul>
<hr>
<h2>JSTL 库安装</h2>
<p>Apache Tomcat安装JSTL 库步骤如下：</p>
<p>从Apache的标准标签库中下载的二进包(jakarta-taglibs-standard-current.zip)。</p>
<ul><li>官方下载地址：<a href="http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/" target="_blank">http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/</a></li>
<li>本站下载地址：<a href="http://static.runoob.com/download/jakarta-taglibs-standard-1.1.2.tar.gz" target="_blank">jakarta-taglibs-standard-1.1.2.zip</a>
</li>
</ul><p>
下载jakarta-taglibs-standard-1.1.2.zip 包并解压，将jakarta-taglibs-standard-1.1.2/lib/下的两个jar文件：standard.jar和jstl.jar文件拷贝到/WEB-INF/lib/下。</li>
</p>
<p>接下来我们在 web.xml 文件中添加以下配置：</p>
<pre>
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
	&lt;jsp-config&gt;
	&lt;taglib&gt;
	&lt;taglib-uri&gt;http://java.sun.com/jstl/fmt&lt;/taglib-uri&gt;
	&lt;taglib-location&gt;/WEB-INF/fmt.tld&lt;/taglib-location&gt;
	&lt;/taglib&gt;
	&lt;taglib&gt;
	&lt;taglib-uri&gt;http://java.sun.com/jstl/fmt-rt&lt;/taglib-uri&gt;
	&lt;taglib-location&gt;/WEB-INF/fmt-rt.tld&lt;/taglib-location&gt;
	&lt;/taglib&gt;
	&lt;taglib&gt;
	&lt;taglib-uri&gt;http://java.sun.com/jstl/core&lt;/taglib-uri&gt;
	&lt;taglib-location&gt;/WEB-INF/c.tld&lt;/taglib-location&gt;
	&lt;/taglib&gt;
	&lt;taglib&gt;
	&lt;taglib-uri&gt;http://java.sun.com/jstl/core-rt&lt;/taglib-uri&gt;
	&lt;taglib-location&gt;/WEB-INF/c-rt.tld&lt;/taglib-location&gt;
	&lt;/taglib&gt;
	&lt;taglib&gt;
	&lt;taglib-uri&gt;http://java.sun.com/jstl/sql&lt;/taglib-uri&gt;
	&lt;taglib-location&gt;/WEB-INF/sql.tld&lt;/taglib-location&gt;
	&lt;/taglib&gt;
	&lt;taglib&gt;
	&lt;taglib-uri&gt;http://java.sun.com/jstl/sql-rt&lt;/taglib-uri&gt;
	&lt;taglib-location&gt;/WEB-INF/sql-rt.tld&lt;/taglib-location&gt;
	&lt;/taglib&gt;
	&lt;taglib&gt;
	&lt;taglib-uri&gt;http://java.sun.com/jstl/x&lt;/taglib-uri&gt;
	&lt;taglib-location&gt;/WEB-INF/x.tld&lt;/taglib-location&gt;
	&lt;/taglib&gt;
	&lt;taglib&gt;
	&lt;taglib-uri&gt;http://java.sun.com/jstl/x-rt&lt;/taglib-uri&gt;
	&lt;taglib-location&gt;/WEB-INF/x-rt.tld&lt;/taglib-location&gt;
	&lt;/taglib&gt;
	&lt;/jsp-config&gt;
&lt;/web-app&gt;
</pre>
<p>使用任何库，你必须在每个JSP文件中的头部包含&lt;taglib&gt;标签。</p>
<hr><h2>
	核心标签</h2><p>
核心标签是最常用的JSTL标签。引用核心标签库的语法如下：</p>
<pre>
&lt;%@ taglib prefix="c" 
           uri="http://java.sun.com/jsp/jstl/core" %&gt;
</pre>
<table class="reference">
	<tbody>
		<tr>
			<th>
				标签</th>
			<th>
				描述</th>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-out-tag.html" target="_blank">&lt;c:out&gt;</a></td>
			<td>
				用于在JSP中显示数据，就像&lt;%= ... &gt;</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-set-tag.html" target="_blank">&lt;c:set&gt;</a></td>
			<td>
				用于保存数据</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-remove-tag.html" target="_blank">&lt;c:remove&gt;</a></td>
			<td>
				用于删除数据</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-catch-tag.html" target="_blank">&lt;c:catch&gt;</a></td>
			<td>
				用来处理产生错误的异常状况，并且将错误信息储存起来</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-if-tag.html" target="_blank">&lt;c:if&gt;</a></td>
			<td>
				与我们在一般程序中用的if一样</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-choose-tag.html" target="_blank">&lt;c:choose&gt;</a></td>
			<td>
				本身只当做&lt;c:when&gt;和&lt;c:otherwise&gt;的父标签</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-choose-tag.html" target="_blank">&lt;c:when&gt;</a></td>
			<td>
				&lt;c:choose&gt;的子标签，用来判断条件是否成立</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-choose-tag.html" target="_blank">&lt;c:otherwise&gt;</a></td>
			<td>
				&lt;c:choose&gt;的子标签，接在&lt;c:when&gt;标签后，当&lt;c:when&gt;标签判断为false时被执行</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-import-tag.html" target="_blank">&lt;c:import&gt;</a></td>
			<td>
				检索一个绝对或相对 URL，然后将其内容暴露给页面</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-foreach-tag.html" target="_blank">&lt;c:forEach&gt;</a></td>
			<td>
				基础迭代标签，接受多种集合类型</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-foreach-tag.html" target="_blank">&lt;c:forTokens&gt;</a></td>
			<td>
				根据指定的分隔符来分隔内容并迭代输出</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-param-tag.html" target="_blank">&lt;c:param&gt;</a></td>
			<td>
				用来给包含或重定向的页面传递参数</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-redirect-tag.html" target="_blank">&lt;c:redirect&gt;</a></td>
			<td>
				重定向至一个新的URL.</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-core-url-tag.html" target="_blank">&lt;c:url&gt;</a></td>
			<td>
				使用可选的查询参数来创造一个URL</td>
		</tr>
	</tbody>
</table>
<hr>
<h2>
	格式化标签</h2>
	<p>
JSTL格式化标签用来格式化并输出文本、日期、时间、数字。引用格式化标签库的语法如下：</p>
<pre>
&lt;%@ taglib prefix="fmt" 
           uri="http://java.sun.com/jsp/jstl/fmt" %&gt;
</pre>
<table class="reference">
	<tbody>
		<tr>
			<th>
				标签</th>
			<th>
				描述</th>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-formatnumber-tag.html" target="_blank">&lt;fmt:formatNumber&gt;</a></td>
			<td>
				使用指定的格式或精度格式化数字</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-parsenumber-tag.html" target="_blank">&lt;fmt:parseNumber&gt;</a></td>
			<td>
				解析一个代表着数字，货币或百分比的字符串</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-formatdate-tag.html" target="_blank">&lt;fmt:formatDate&gt;</a></td>
			<td>
				使用指定的风格或模式格式化日期和时间</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-parsedate-tag.html" target="_blank">&lt;fmt:parseDate&gt;</a></td>
			<td>
				解析一个代表着日期或时间的字符串</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-bundle-tag.html" target="_blank">&lt;fmt:bundle&gt;</a></td>
			<td>
				绑定资源</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-setlocale-tag.html" target="_blank">&lt;fmt:setLocale&gt;</a></td>
			<td>
				指定地区</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-setbundle-tag.html" target="_blank">&lt;fmt:setBundle&gt;</a></td>
			<td>
				绑定资源</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-timezone-tag.html" target="_blank">&lt;fmt:timeZone&gt;</a></td>
			<td>
				指定时区</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-settimezone-tag.html" target="_blank">&lt;fmt:setTimeZone&gt;</a></td>
			<td>
				指定时区</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-message-tag.html" target="_blank">&lt;fmt:message&gt;</a></td>
			<td>
				显示资源配置文件信息</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-format-requestencoding-tag.html" target="_blank">&lt;fmt:requestEncoding&gt;</a></td>
			<td>
				设置request的字符编码</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	SQL标签</h2>
	<p>
JSTL SQL标签库提供了与关系型数据库（Oracle，MySQL，SQL Server等等）进行交互的标签。引用SQL标签库的语法如下：</p>
<pre>
&lt;%@ taglib prefix="sql" 
           uri="http://java.sun.com/jsp/jstl/sql" %&gt;
</pre>
<table class="reference">
	<tbody>
		<tr>
			<th>
				标签</th>
			<th>
				描述</th>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-sql-setdatasource-tag.html" target="_blank">&lt;sql:setDataSource&gt;</a></td>
			<td>
				指定数据源</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-sql-query-tag.html" target="_blank">&lt;sql:query&gt;</a></td>
			<td>
				运行SQL查询语句</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-sql-update-tag.html" target="_blank">&lt;sql:update&gt;</a></td>
			<td>
				运行SQL更新语句</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-sql-param-tag.html" target="_blank">&lt;sql:param&gt;</a></td>
			<td>
				将SQL语句中的参数设为指定值</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-sql-dateparam-tag.html" target="_blank">&lt;sql:dateParam&gt;</a></td>
			<td>
				将SQL语句中的日期参数设为指定的java.util.Date&nbsp;对象值</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-sql-transaction-tag.html" target="_blank">&lt;sql:transaction&gt;</a></td>
			<td>
				在共享数据库连接中提供嵌套的数据库行为元素，将所有语句以一个事务的形式来运行</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	XML 标签</h2><p>
JSTL XML标签库提供了创建和操作XML文档的标签。引用XML标签库的语法如下：</p>
<pre>
&lt;%@ taglib prefix="x" 
           uri="http://java.sun.com/jsp/jstl/xml" %&gt;
</pre>
<p>在使用xml标签前，你必须将XML 和 XPath 的相关包拷贝至你的&lt;Tomcat 安装目录&gt;\lib下:</p>
<ul>
<li><b>XercesImpl.jar</b><p>下载地址： <a href="http://www.apache.org/dist/xerces/j/" target="_blank">http://www.apache.org/dist/xerces/j/</a></p></li>
<li><b>xalan.jar</b><p> 下载地址： <a href="http://xml.apache.org/xalan-j/index.html" target="_blank">http://xml.apache.org/xalan-j/index.html</a></p></li>
</ul>
<table class="reference">
	<tbody>
		<tr>
			<th>
				标签</th>
			<th>
				描述</th>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-out-tag.html" target="_blank">&lt;x:out&gt;</a></td>
			<td>
				与&lt;%= ... &gt;,类似，不过只用于XPath表达式</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-parse-tag.html" target="_blank">&lt;x:parse&gt;</a></td>
			<td>
				解析 XML 数据</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-set-tag.html" target="_blank">&lt;x:set&gt;</a></td>
			<td>
				设置XPath表达式</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-if-tag.html" target="_blank">&lt;x:if&gt;</a></td>
			<td>
				判断XPath表达式，若为真，则执行本体中的内容，否则跳过本体</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-foreach-tag.html" target="_blank">&lt;x:forEach&gt;</a></td>
			<td>
				迭代XML文档中的节点</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-choose-tag.html" target="_blank">&lt;x:choose&gt;</a></td>
			<td>
				&lt;x:when&gt;和&lt;x:otherwise&gt;的父标签</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-choose-tag.html" target="_blank">&lt;x:when&gt;</a></td>
			<td>
				&lt;x:choose&gt;的子标签，用来进行条件判断</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-choose-tag.html" target="_blank">&lt;x:otherwise&gt;</a></td>
			<td>
				&lt;x:choose&gt;的子标签，当&lt;x:when&gt;判断为false时被执行</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-transform-tag.html" target="_blank">&lt;x:transform&gt;</a></td>
			<td>
				将XSL转换应用在XML文档中</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-xml-param-tag.html" target="_blank">&lt;x:param&gt;</a></td>
			<td>
				与&lt;x:transform&gt;共同使用，用于设置XSL样式表</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	JSTL函数</h2><p>
JSTL包含一系列标准函数，大部分是通用的字符串处理函数。引用JSTL函数库的语法如下：</p>
<pre>
&lt;%@ taglib prefix="fn" 
           uri="http://java.sun.com/jsp/jstl/functions" %&gt;
</pre>
<table class="reference">
	<tbody>
		<tr>
			<th>
				函数</th>
			<th>
				描述</th>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-contains.html" target="_blank">fn:contains()</a></td>
			<td>
				测试输入的字符串是否包含指定的子串</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-containsignoreCase.html" target="_blank">fn:containsIgnoreCase()</a></td>
			<td>
				测试输入的字符串是否包含指定的子串，大小写不敏感</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-endswith.html" target="_blank">fn:endsWith()</a></td>
			<td>
				测试输入的字符串是否以指定的后缀结尾</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-escapexml.html" target="_blank">fn:escapeXml()</a></td>
			<td>
				跳过可以作为XML标记的字符</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-indexof.html" target="_blank">fn:indexOf()</a></td>
			<td>
				返回指定字符串在输入字符串中出现的位置</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-join.html" target="_blank">fn:join()</a></td>
			<td>
				将数组中的元素合成一个字符串然后输出</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-length.html" target="_blank">fn:length()</a></td>
			<td>
				返回字符串长度</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-replace.html" target="_blank">fn:replace()</a></td>
			<td>
				将输入字符串中指定的位置替换为指定的字符串然后返回</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-split.html" target="_blank">fn:split()</a></td>
			<td>
				将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-startswith.html" target="_blank">fn:startsWith()</a></td>
			<td>
				测试输入字符串是否以指定的前缀开始</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-substring.html" target="_blank">fn:substring()</a></td>
			<td>
				返回字符串的子集</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-substringafter.html" target="_blank">fn:substringAfter()</a></td>
			<td>
				返回字符串在指定子串之后的子集</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-substringbefore.html" target="_blank">fn:substringBefore()</a></td>
			<td>
				返回字符串在指定子串之前的子集</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-tolowercase.html" target="_blank">fn:toLowerCase()</a></td>
			<td>
				将字符串中的字符转为小写</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-touppercase.html" target="_blank">fn:toUpperCase()</a></td>
			<td>
				将字符串中的字符转为大写</td>
		</tr>
		<tr>
			<td>
				<a href="/jsp/jstl-function-trim.html" target="_blank">fn:trim()</a></td>
			<td>
				移除首位的空白符</td>
		</tr>
	</tbody>
</table>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jstl-function-trim.html" rel="prev"> JSTL fn:trim()函数</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/eclipse-jsp.html" rel="next"> Eclipse JSP/Servlet 环境搭建</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/python3/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-sending-email.html" rel="prev"> JSP 发送邮件</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-xml-data.html" rel="next"> JSP XML 数据处理</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">连接数据库</span></h1>

<div class="tutintro">
<p>本教程假定您已经了解了 JDBC 应用程序的工作方式。在您开始学习 JSP 数据库访问之前，请访问 <a href="http://www.runoob.com/java/java-mysql-connect.html">Java MySQL 连接</a> 来设置相关驱动及配置。</p>
<blockquote>
<p><b>注意：</b></p>
<p>你可以下载本站提供的 jar 包：<strong><a href="http://static.runoob.com/download/mysql-connector-java-5.1.39-bin.jar" target="_blank">mysql-connector-java-5.1.39-bin.jar</a></strong></p>
<p>下载后把 mysql-connector-java-5.1.39-bin.jar 拷贝到 tomcat 下 lib 目录。
</p>

</blockquote>
<p>从基本概念下手，让我们来创建一个简单的表，并在表中创建几条记录。</p>
</div>
<hr>
<h2>创建测试数据</h2>
<p>接下来我们在 MySQL 中创建 RUNOOB 数据库，并创建 websites 数据表，表结构如下：</p>
<pre>
CREATE TABLE `websites` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
  `url` varchar(255) NOT NULL DEFAULT '',
  `alexa` int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
  `country` char(10) NOT NULL DEFAULT '' COMMENT '国家',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
</pre>
<p>插入一些数据：</p>
<pre>
INSERT INTO `websites` VALUES ('1', 'Google', 'https://www.google.cm/', '1', 'USA'), ('2', '淘宝', 'https://www.taobao.com/', '13', 'CN'), ('3', '菜鸟教程', 'http://www.runoob.com', '5892', ''), ('4', '微博', 'http://weibo.com/', '20', 'CN'), ('5', 'Facebook', 'https://www.facebook.com/', '3', 'USA');
</pre>
<p>数据表显示如下：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2013/12/mysql-data.jpg"></p><hr>
<hr>

<hr>
<h2>SELECT操作</h2><p>
接下来的这个例子告诉我们如何使用JSTL SQL标签来运行SQL SELECT语句：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*,java.sql.*"%&gt;
&lt;%@ page import="javax.servlet.http.*,javax.servlet.*" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%&gt;
 
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;SELECT 操作&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!--
JDBC 驱动名及数据库 URL 
数据库的用户名与密码，需要根据自己的设置
useUnicode=true&amp;characterEncoding=utf-8 防止中文乱码
 --&gt;
&lt;sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/RUNOOB?useUnicode=true&amp;characterEncoding=utf-8"
     user="root"  password="123456"/&gt;
 
&lt;sql:query dataSource="${snapshot}" var="result"&gt;
SELECT * from websites;
&lt;/sql:query&gt;
&lt;h1&gt;JSP 数据库实例 - 菜鸟教程&lt;/h1&gt;
&lt;table border="1" width="100%"&gt;
&lt;tr&gt;
   &lt;th&gt;ID&lt;/th&gt;
   &lt;th&gt;站点名&lt;/th&gt;
   &lt;th&gt;站点地址&lt;/th&gt;
&lt;/tr&gt;
&lt;c:forEach var="row" items="${result.rows}"&gt;
&lt;tr&gt;
   &lt;td&gt;&lt;c:out value="${row.id}"/&gt;&lt;/td&gt;
   &lt;td&gt;&lt;c:out value="${row.name}"/&gt;&lt;/td&gt;
   &lt;td&gt;&lt;c:out value="${row.url}"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
 
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
访问这个JSP例子，运行结果如下：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jspmysql1.jpg" width="50%" /></p><hr>
<h2>
	INSERT操作</h2><p>
这个例子告诉我们如何使用JSTL SQL标签来运行SQL INSERT语句：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*,java.sql.*"%&gt;
&lt;%@ page import="javax.servlet.http.*,javax.servlet.*" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%&gt;
 
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;SELECT 操作&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!--
JDBC 驱动名及数据库 URL 
数据库的用户名与密码，需要根据自己的设置
useUnicode=true&amp;characterEncoding=utf-8 防止中文乱码
 --&gt;
&lt;sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/RUNOOB?useUnicode=true&amp;characterEncoding=utf-8"
     user="root"  password="123456"/&gt;
&lt;!--
插入数据
 --&gt;
&lt;sql:update dataSource="${snapshot}" var="result"&gt;
INSERT INTO websites (name,url,alexa,country) VALUES ('菜鸟教程移动站', 'http://m.runoob.com', 5093, 'CN');
&lt;/sql:update&gt;
&lt;sql:query dataSource="${snapshot}" var="result"&gt;
SELECT * from websites;
&lt;/sql:query&gt;
&lt;h1&gt;JSP 数据库实例 - 菜鸟教程&lt;/h1&gt;
&lt;table border="1" width="100%"&gt;
&lt;tr&gt;
   &lt;th&gt;ID&lt;/th&gt;
   &lt;th&gt;站点名&lt;/th&gt;
   &lt;th&gt;站点地址&lt;/th&gt;
&lt;/tr&gt;
&lt;c:forEach var="row" items="${result.rows}"&gt;
&lt;tr&gt;
   &lt;td&gt;&lt;c:out value="${row.id}"/&gt;&lt;/td&gt;
   &lt;td&gt;&lt;c:out value="${row.name}"/&gt;&lt;/td&gt;
   &lt;td&gt;&lt;c:out value="${row.url}"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
 
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
访问这个JSP例子，运行结果如下：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jspmysql2.jpg" width="50%"/></p><hr>
<h2>
	DELETE操作</h2><p>
这个例子告诉我们如何使用JSTL SQL标签来运行SQL DELETE语句：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*,java.sql.*"%&gt;
&lt;%@ page import="javax.servlet.http.*,javax.servlet.*" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%&gt;
 
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;SELECT 操作&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!--
JDBC 驱动名及数据库 URL 
数据库的用户名与密码，需要根据自己的设置
useUnicode=true&amp;characterEncoding=utf-8 防止中文乱码
 --&gt;
&lt;sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/RUNOOB?useUnicode=true&amp;characterEncoding=utf-8"
     user="root"  password="123456"/&gt;

&lt;!--
删除 ID 为 11 的数据
 --&gt;
&lt;sql:update dataSource="${snapshot}" var="count"&gt;
  DELETE FROM websites WHERE Id = ?
  &lt;sql:param value="${11}" /&gt;
&lt;/sql:update&gt;

&lt;sql:query dataSource="${snapshot}" var="result"&gt;
SELECT * from websites;
&lt;/sql:query&gt;
&lt;h1&gt;JSP 数据库实例 - 菜鸟教程&lt;/h1&gt;
&lt;table border="1" width="100%"&gt;
&lt;tr&gt;
   &lt;th&gt;ID&lt;/th&gt;
   &lt;th&gt;站点名&lt;/th&gt;
   &lt;th&gt;站点地址&lt;/th&gt;
&lt;/tr&gt;
&lt;c:forEach var="row" items="${result.rows}"&gt;
&lt;tr&gt;
   &lt;td&gt;&lt;c:out value="${row.id}"/&gt;&lt;/td&gt;
   &lt;td&gt;&lt;c:out value="${row.name}"/&gt;&lt;/td&gt;
   &lt;td&gt;&lt;c:out value="${row.url}"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
 
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
访问这个JSP例子，运行结果如下：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jspmysql1.jpg" width="50%"/></p><hr>
<h2>UPDATE操作</h2><p>
这个例子告诉我们如何使用JSTL SQL标签来运行SQL UPDATE语句：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ page import="java.io.*,java.util.*,java.sql.*"%&gt;
&lt;%@ page import="javax.servlet.http.*,javax.servlet.*" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%&gt;
 
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;SELECT 操作&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!--
JDBC 驱动名及数据库 URL 
数据库的用户名与密码，需要根据自己的设置
useUnicode=true&amp;characterEncoding=utf-8 防止中文乱码
 --&gt;
&lt;sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost:3306/RUNOOB?useUnicode=true&amp;characterEncoding=utf-8"
     user="root"  password="123456"/&gt;

&lt;!--
修改 ID 为 3 的名字：菜鸟教程改为 RUNOOB
 --&gt;
&lt;c:set var="SiteId" value="3"/&gt;
 
&lt;sql:update dataSource="${snapshot}" var="count"&gt;
  UPDATE websites SET name = 'RUNOOB' WHERE Id = ?
  &lt;sql:param value="${SiteId}" /&gt;
&lt;/sql:update&gt;

&lt;sql:query dataSource="${snapshot}" var="result"&gt;
SELECT * from websites;
&lt;/sql:query&gt;
&lt;h1&gt;JSP 数据库实例 - 菜鸟教程&lt;/h1&gt;
&lt;table border="1" width="100%"&gt;
&lt;tr&gt;
   &lt;th&gt;ID&lt;/th&gt;
   &lt;th&gt;站点名&lt;/th&gt;
   &lt;th&gt;站点地址&lt;/th&gt;
&lt;/tr&gt;
&lt;c:forEach var="row" items="${result.rows}"&gt;
&lt;tr&gt;
   &lt;td&gt;&lt;c:out value="${row.id}"/&gt;&lt;/td&gt;
   &lt;td&gt;&lt;c:out value="${row.name}"/&gt;&lt;/td&gt;
   &lt;td&gt;&lt;c:out value="${row.url}"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
 
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
访问这个JSP例子，运行结果如下：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jspmysql3.jpg" width="50%"/></p>
			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-sending-email.html" rel="prev"> JSP 发送邮件</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-xml-data.html" rel="next"> JSP XML 数据处理</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-database-access.html" rel="prev"> JSP 连接数据库</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-javabean.html" rel="next"> JSP JavaBean</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">XML 数据处理</span></h1>
<p>当通过HTTP发送XML数据时，就有必要使用JSP来处理传入和流出的XML文档了，比如RSS文档。作为一个XML文档，它仅仅只是一堆文本而已，使用JSP创建XML文档并不比创建一个HTML文档难。</p>
<hr>
<h2>
	使用JSP发送XML</h2><p>
使用JSP发送XML内容就和发送HTML内容一样。唯一的不同就是您需要把页面的context属性设置为text/xml。要设置context属性，使用&lt;%@page %&nbsp;&gt;命令，就像这样：</p>
<pre>
&lt;%@ page contentType="text/xml" %&gt;
</pre>
<p>
接下来这个例子向浏览器发送XML内容：</p>
<pre>
&lt;%@ page contentType="text/xml" %&gt;

&lt;books&gt;
   &lt;book&gt;
      &lt;name&gt;Padam History&lt;/name&gt;
      &lt;author&gt;ZARA&lt;/author&gt;
      &lt;price&gt;100&lt;/price&gt;
   &lt;/book&gt;
&lt;/books&gt;
</pre>
<p>
使用不同的浏览器来访问这个例子，看看这个例子所呈现的文档树。</p><hr>
<h2>
	在JSP中处理XML</h2><p>
在使用JSP处理XML之前，您需要将与XML 和XPath相关的两个库文件放在&lt;Tomcat Installation Directory&gt;\lib目录下：</p>
<ul>
	<li>
		XercesImpl.jar：在这下载<a href="http://www.apache.org/dist/xerces/j/" target="_blank">http://www.apache.org/dist/xerces/j/</a></li>
	<li>
		xalan.jar：在这下载<a href="http://xml.apache.org/xalan-j/index.html" target="_blank">http://xml.apache.org/xalan-j/index.html</a></li>
</ul>
<p>books.xml文件:</p>
<pre>
&lt;books&gt;
&lt;book&gt;
  &lt;name&gt;Padam History&lt;/name&gt;
  &lt;author&gt;ZARA&lt;/author&gt;
  &lt;price&gt;100&lt;/price&gt;
&lt;/book&gt;
&lt;book&gt;
  &lt;name&gt;Great Mistry&lt;/name&gt;
  &lt;author&gt;NUHA&lt;/author&gt;
  &lt;price&gt;2000&lt;/price&gt;
&lt;/book&gt;
&lt;/books&gt;
</pre>
<p>
main.jsp文件：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
&lt;%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %&gt;
 
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;JSTL x:parse Tags&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;Books Info:&lt;/h3&gt;
&lt;c:import var="bookInfo" url="http://localhost:8080/books.xml"/&gt;
 
&lt;x:parse xml="${bookInfo}" var="output"/&gt;
&lt;b&gt;The title of the first book is&lt;/b&gt;: 
&lt;x:out select="$output/books/book[1]/name" /&gt;
&lt;br&gt;
&lt;b&gt;The price of the second book&lt;/b&gt;: 
&lt;x:out select="$output/books/book[2]/price" /&gt;
 
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
访问http://localhost:8080/main.jsp，运行结果如下：</p>
<pre>
BOOKS INFO:
The title of the first book is:Padam History 
The price of the second book: 2000
</pre><hr>
<h2>
	使用JSP格式化XML</h2><p>
这个是XSLT样式表style.xsl文件：</p>
<pre>
&lt;?xml version="1.0"?&gt;
&lt;xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
 
&lt;xsl:output method="html" indent="yes"/&gt;
 
&lt;xsl:template match="/"&gt;
  &lt;html&gt;
  &lt;body&gt;
   &lt;xsl:apply-templates/&gt;
  &lt;/body&gt;
  &lt;/html&gt;
&lt;/xsl:template&gt;
 
&lt;xsl:template match="books"&gt;
  &lt;table border="1" width="100%"&gt;
    &lt;xsl:for-each select="book"&gt;
      &lt;tr&gt;
        &lt;td&gt;
          &lt;i&gt;&lt;xsl:value-of select="name"/&gt;&lt;/i&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;xsl:value-of select="author"/&gt;
        &lt;/td&gt;
        &lt;td&gt;
          &lt;xsl:value-of select="price"/&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/xsl:for-each&gt;
  &lt;/table&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;
</pre>
<p>
这个是main.jsp文件：</p>
<pre>
&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
&lt;%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %&gt;
 
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;JSTL x:transform Tags&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;Books Info:&lt;/h3&gt;
&lt;c:set var="xmltext"&gt;
  &lt;books&gt;
    &lt;book&gt;
      &lt;name&gt;Padam History&lt;/name&gt;
      &lt;author&gt;ZARA&lt;/author&gt;
      &lt;price&gt;100&lt;/price&gt;
    &lt;/book&gt;
    &lt;book&gt;
      &lt;name&gt;Great Mistry&lt;/name&gt;
      &lt;author&gt;NUHA&lt;/author&gt;
      &lt;price&gt;2000&lt;/price&gt;
    &lt;/book&gt;
  &lt;/books&gt;
&lt;/c:set&gt;
 
&lt;c:import url="http://localhost:8080/style.xsl" var="xslt"/&gt;
&lt;x:transform xml="${xmltext}" xslt="${xslt}"/&gt;
 
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
运行结果如下：</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/xml-1.jpg" /><p><p>
更多关于使用JSTL处理XML的内容请查阅<a href="/jsp/jsp-jstl.html" target="_blank">JSP标准标签库</a>。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-database-access.html" rel="prev"> JSP 连接数据库</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-javabean.html" rel="next"> JSP JavaBean</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-xml-data.html" rel="prev"> JSP XML 数据处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-custom-tags.html" rel="next"> JSP 自定义标签</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">JavaBean</span></h1>
<p>JavaBean是特殊的Java类，使用J ava语言书写，并且遵守JavaBean API规范。</p>
<p>
接下来给出的是JavaBean与其它Java类相比而言独一无二的特征：</p>
<ul>
	<li>
		提供一个默认的无参构造函数。</li>
	<li>
		需要被序列化并且实现了Serializable接口。</li>
	<li>
		可能有一系列可读写属性。</li>
	<li>
		可能有一系列的"getter"或"setter"方法。</li>
</ul><hr>
<h2>JavaBean属性</h2><p>
一个JavaBean对象的属性应该是可访问的。这个属性可以是任意合法的Java数据类型，包括自定义Java类。</p>
<p>
一个JavaBean对象的属性可以是可读写，或只读，或只写。JavaBean对象的属性通过JavaBean实现类中提供的两个方法来访问：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>方法</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				get<strong>PropertyName</strong>()</td>
			<td>
				举例来说，如果属性的名称为myName，那么这个方法的名字就要写成getMyName()来读取这个属性。这个方法也称为访问器。</td>
		</tr>
		<tr>
			<td>
				set<strong>PropertyName</strong>()</td>
			<td>
				举例来说，如果属性的名称为myName，那么这个方法的名字就要写成setMyName()来写入这个属性。这个方法也称为写入器。</td>
		</tr>
	</tbody>
</table>
<p>
一个只读的属性只提供getPropertyName()方法，一个只写的属性只提供setPropertyName()方法。</p><hr>
<h2>JavaBean 程序示例</h2><p>
这是StudentBean.java文件：</p>
<pre>
package com.runoob;

public class StudentsBean implements java.io.Serializable
{
   private String firstName = null;
   private String lastName = null;
   private int age = 0;

   public StudentsBean() {
   }
   public String getFirstName(){
      return firstName;
   }
   public String getLastName(){
      return lastName;
   }
   public int getAge(){
      return age;
   }

   public void setFirstName(String firstName){
      this.firstName = firstName;
   }
   public void setLastName(String lastName){
      this.lastName = lastName;
   }
   public void setAge(int age) {
      this.age = age;
   }
}
</pre>

<p>编译 StudentBean.java 文件(最后一个实例会用到):</p>
<pre>
$ javac StudentsBean.java
</pre>
<p>编译后获得 <strong>StudentBean.class</strong> 文件，将其拷贝到 <strong>&lt;JSP 项目&gt;/WebContent/WEB-INF/classes/com/runoob</strong>，如下图所示:</p>
<p><img width="70%" src="http://www.runoob.com/wp-content/uploads/2014/01/DDBE2229-22EF-45A5-B64A-EA1B74C7F43E.jpg"></p>
<hr>
<h2>访问JavaBean</h2><p>
&lt;jsp:useBean&gt; 标签可以在JSP中声明一个JavaBean，然后使用。声明后，JavaBean对象就成了脚本变量，可以通过脚本元素或其他自定义标签来访问。&lt;jsp:useBean&gt;标签的语法格式如下：
</p><pre>
&lt;jsp:useBean id="bean 的名字" scope="bean 的作用域" typeSpec/&gt;
</pre>
<p>
其中，根据具体情况，scope的值可以是page，request，session或application。id值可任意只要不和同一JSP文件中其它&lt;jsp:useBean&gt;中id值一样就行了。</p>
<p>
接下来给出的是 &lt;jsp:useBean&gt; 标签的一个简单的用法：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;useBean 实例&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;jsp:useBean id="date" class="java.util.Date" /&gt; 
&lt;p&gt;日期为：&lt;%= date %&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre><p>
它将会产生如下结果：</p>
<pre>
日期为：Tue Jun 28 15:22:24 CST 2016
</pre><hr>
<h2>访问 JavaBean 对象的属性</h2><p>
在 <strong>&lt;jsp:useBean&gt;</strong> 标签主体中使用 <strong>&lt;jsp:getProperty/&gt;</strong> 标签来调用 <strong>getter</strong> 方法，使用 <strong>&lt;jsp:setProperty/&gt;</strong> 标签来调用 <strong>setter</strong> 方法，语法格式如下：</p>
<pre>
&lt;jsp:useBean id="id" class="bean 编译的类" scope="bean 作用域"&gt;
   &lt;jsp:setProperty name="bean 的 id" property="属性名"  
                    value="value"/&gt;
   &lt;jsp:getProperty name="bean 的 id" property="属性名"/&gt;
   ...........
&lt;/jsp:useBean&gt;
</pre>
<p>
name属性指的是Bean的id属性。property属性指的是想要调用的getter或setter方法。</p>
<p>
接下来给出使用以上语法进行属性访问的一个简单例子：</p>
<pre>
&lt;%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;get 和 set 属性实例&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;jsp:useBean id="students" 
                    class="com.runoob.StudentsBean"&gt; 
   &lt;jsp:setProperty name="students" property="firstName"
                    value="小强"/&gt;
   &lt;jsp:setProperty name="students" property="lastName" 
                    value="王"/&gt;
   &lt;jsp:setProperty name="students" property="age"
                    value="10"/&gt;
&lt;/jsp:useBean&gt;

&lt;p&gt;学生名字: 
   &lt;jsp:getProperty name="students" property="firstName"/&gt;
&lt;/p&gt;
&lt;p&gt;学生姓氏: 
   &lt;jsp:getProperty name="students" property="lastName"/&gt;
&lt;/p&gt;
&lt;p&gt;学生年龄: 
   &lt;jsp:getProperty name="students" property="age"/&gt;
&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
访问以上 JSP，运行结果如下：</p>
<pre>
学生名字: 小强

学生姓氏: 王

学生年龄: 10
</pre>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-xml-data.html" rel="prev"> JSP XML 数据处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-custom-tags.html" rel="next"> JSP 自定义标签</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-javabean.html" rel="prev"> JSP JavaBean</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-expression-language.html" rel="next"> JSP 表达式语言</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">自定义标签</span></h1>
<p>自定义标签是用户定义的JSP语言元素。当JSP页面包含一个自定义标签时将被转化为servlet，标签转化为对被
    称为tag handler的对象的操作，即当servlet执行时Web container调用那些操作。</p>
<p>JSP标签扩展可以让你创建新的标签并且可以直接插入到一个JSP页面。 JSP 2.0规范中引入Simple Tag Handlers来编写这些自定义标记。</p>
<p>你可以继承SimpleTagSupport类并重写的doTag()方法来开发一个最简单的自定义标签。</p>
<hr>
<h2>创建"Hello"标签</h2>
<p>接下来，我们想创建一个自定义标签叫作&lt;ex:Hello&gt;，标签格式为：</p>
<pre>
&lt;ex:Hello /&gt;
</pre>
<p>要创建自定义的JSP标签，你首先必须创建处理标签的Java类。所以，让我们创建一个HelloTag类，如下所示：</p>
<pre>
package com.runoob;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

  public void doTag() throws JspException, IOException {
    JspWriter out = getJspContext().getOut();
    out.println("Hello Custom Tag!");
  }
}
</pre>
<p>以下代码重写了doTag()方法，方法中使用了getJspContext()方法来获取当前的JspContext对象，并将"Hello Custom Tag!"传递给JspWriter对象。</p>
<p>编译以上类，并将其复制到环境变量CLASSPATH目录中。最后创建如下标签库：&lt;Tomcat安装目录&gt;webapps\ROOT\WEB-INF\custom.tld。</p>

<pre>
&lt;taglib&gt;
  &lt;tlib-version&gt;1.0&lt;/tlib-version&gt;
  &lt;jsp-version&gt;2.0&lt;/jsp-version&gt;
  &lt;short-name&gt;Example TLD&lt;/short-name&gt;
  &lt;tag&gt;
    &lt;name&gt;Hello&lt;/name&gt;
    &lt;tag-class&gt;com.runoob.HelloTag&lt;/tag-class&gt;
    &lt;body-content&gt;empty&lt;/body-content&gt;
  &lt;/tag&gt;
&lt;/taglib&gt;
</pre>
<p>接下来，我们就可以在JSP文件中使用Hello标签：</p>
<pre>
&lt;%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;A sample custom tag&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;ex:Hello/&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>以上程序输出结果为：</p>
<pre>
Hello Custom Tag!
</pre>
<hr>
<h2>访问标签体</h2>
<p>你可以像标准标签库一样在标签中包含消息内容。如我们要在我们自定义的Hello中包含内容，格式如下：</p>
<pre>
&lt;ex:Hello&gt;
   This is message body
&lt;/ex:Hello&gt;
</pre>
<p>我们可以修改标签处理类文件，代码如下：</p>
<pre>
package com.runoob;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

   StringWriter sw = new StringWriter();
   public void doTag()
      throws JspException, IOException
    {
       getJspBody().invoke(sw);
       getJspContext().getOut().println(sw.toString());
    }

}
</pre>
<p>接下来我们需要修改TLD文件，如下所示：
</p>
<pre>
&lt;taglib&gt;
  &lt;tlib-version&gt;1.0&lt;/tlib-version&gt;
  &lt;jsp-version&gt;2.0&lt;/jsp-version&gt;
  &lt;short-name&gt;Example TLD with Body&lt;/short-name&gt;
  &lt;tag&gt;
    &lt;name&gt;Hello&lt;/name&gt;
    &lt;tag-class&gt;com.runoob.HelloTag&lt;/tag-class&gt;
    &lt;body-content&gt;scriptless&lt;/body-content&gt;
  &lt;/tag&gt;
&lt;/taglib&gt;
</pre>
<p>现在我们可以在JSP使用修改后的标签，如下所示:</p>
<pre>
&lt;%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;A sample custom tag&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;ex:Hello&gt;
        This is message body
    &lt;/ex:Hello&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>以上程序输出结果如下所示：</p>
<pre>
This is message body
</pre>
<hr>
<h2>自定义标签属性</h2>
<p>你可以在自定义标准中设置各种属性，要接收属性，值自定义标签类必须实现setter方法， JavaBean 中的setter方法如下所示：</p>
<pre>
package com.runoob;

import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class HelloTag extends SimpleTagSupport {

   private String message;

   public void setMessage(String msg) {
      this.message = msg;
   }

   StringWriter sw = new StringWriter();

   public void doTag()
      throws JspException, IOException
    {
       if (message != null) {
          /* 从属性中使用消息 */
          JspWriter out = getJspContext().getOut();
          out.println( message );
       }
       else {
          /* 从内容体中使用消息 */
          getJspBody().invoke(sw);
          getJspContext().getOut().println(sw.toString());
       }
   }

}
</pre>
<p>属性的名称是"message"，所以setter方法​​是的setMessage()。现在让我们在TLD文件中使用的&lt;attribute&gt;元素添加此属性：</p>
<pre>
&lt;taglib&gt;
  &lt;tlib-version&gt;1.0&lt;/tlib-version&gt;
  &lt;jsp-version&gt;2.0&lt;/jsp-version&gt;
  &lt;short-name&gt;Example TLD with Body&lt;/short-name&gt;
  &lt;tag&gt;
    &lt;name&gt;Hello&lt;/name&gt;
    &lt;tag-class&gt;com.runoob.HelloTag&lt;/tag-class&gt;
    &lt;body-content&gt;scriptless&lt;/body-content&gt;
    &lt;attribute&gt;
       &lt;name&gt;message&lt;/name&gt;
    &lt;/attribute&gt;
  &lt;/tag&gt;
&lt;/taglib&gt;
</pre>
<p>现在我们就可以在JSP文件中使用message属性了，如下所示：</p>
<pre>
&lt;%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;A sample custom tag&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;ex:Hello message="This is custom tag" /&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>以上实例数据输出结果为：</p>
<pre>
This is custom tag
</pre>
<p>你还可以包含以下属性：</p>
<table class="reference">
<tbody><tr><th style="width:30%">属性 </th><th>描述</th></tr>
<tr><td>name</td><td>定义属性的名称。每个标签的是属性名称必须是唯一的。</td></tr>
<tr><td>required</td><td>指定属性是否是必须的或者可选的,如果设置为false为可选。</td></tr>
<tr><td>rtexprvalue</td><td>声明在运行表达式时，标签属性是否有效。</td></tr>
<tr><td>type</td><td>定义该属性的Java类类型 。默认指定为 <b>String</b></td></tr>
<tr><td>description</td><td>描述信息</td></tr>
<tr><td>fragment</td><td>如果声明了该属性,属性值将被视为一个 <b>JspFragment</b>。</td></tr>
</tbody></table>
<p>以下是指定相关的属性实例：</p>
<pre>
.....
    &lt;attribute&gt;
      &lt;name&gt;attribute_name&lt;/name&gt;
      &lt;required&gt;false&lt;/required&gt;
      &lt;type&gt;java.util.Date&lt;/type&gt;
      &lt;fragment&gt;false&lt;/fragment&gt;
    &lt;/attribute&gt;
.....
</pre>
<p>如果你使用了两个属性，修改TLD文件，如下所示：</p>
<pre>
.....
    &lt;attribute&gt;
      &lt;name&gt;attribute_name1&lt;/name&gt;
      &lt;required&gt;false&lt;/required&gt;
      &lt;type&gt;java.util.Boolean&lt;/type&gt;
      &lt;fragment&gt;false&lt;/fragment&gt;
    &lt;/attribute&gt;
    &lt;attribute&gt;
      &lt;name&gt;attribute_name2&lt;/name&gt;
      &lt;required&gt;true&lt;/required&gt;
      &lt;type&gt;java.util.Date&lt;/type&gt;
    &lt;/attribute&gt;
.....
</pre>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-javabean.html" rel="prev"> JSP JavaBean</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-expression-language.html" rel="next"> JSP 表达式语言</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-noop.html" title="jQuery.noop()  方法">jQuery.noop()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-custom-tags.html" rel="prev"> JSP 自定义标签</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-exception-handling.html" rel="next"> JSP 异常处理</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">表达式语言</span></h1>
<p>JSP表达式语言（EL）使得访问存储在JavaBean中的数据变得非常简单。JSP EL既可以用来创建算术表达式也可以用来创建逻辑表达式。在JSP EL表达式内可以使用整型数，浮点数，字符串，常量true、false，还有null。</p>
<hr><h2>
	一个简单的语法</h2><p>
典型的，当您需要在JSP标签中指定一个属性值时，只需要简单地使用字符串即可：</p>
<pre>
&lt;jsp:setProperty name="box" property="perimeter" value="100"/&gt;
</pre>
<p>
JSP EL允许您指定一个表达式来表示属性值。一个简单的表达式语法如下：</p>
<pre>
${expr}
</pre>
<p>
其中，expr指的是表达式。在JSP EL中通用的操作符是"."和"[]"。这两个操作符允许您通过内嵌的JSP对象访问各种各样的JavaBean属性。</p>
<p>
举例来说，上面的&lt;jsp:setProperty&gt;标签可以使用表达式语言改写成如下形式：</p>
<pre>
&lt;jsp:setProperty name="box" property="perimeter" 
                 value="${2*box.width+2*box.height}"/&gt;
</pre>
<p>
当JSP编译器在属性中见到"${}"格式后，它会产生代码来计算这个表达式，并且产生一个替代品来代替表达式的值。</p>
<p>
您也可以在标签的模板文本中使用表达式语言。比如&lt;jsp:text&gt;标签简单地将其主体中的文本插入到JSP输出中：</p>
<pre>
&lt;jsp:text&gt;
&lt;h1&gt;Hello JSP!&lt;/h1&gt;
&lt;/jsp:text&gt;
</pre>
<p>
现在，在&lt;jsp:text&gt;标签主体中使用表达式，就像这样：</p>
<pre>
&lt;jsp:text&gt;
Box Perimeter is: ${2*box.width + 2*box.height}
&lt;/jsp:text&gt;
</pre>
<p>
在EL表达式中可以使用圆括号来组织子表达式。比如${(1 + 2) * 3}等于9，但是${1 + (2 * 3)} 等于7。</p>
<p>
想要停用对EL表达式的评估的话，需要使用page指令将isELIgnored属性值设为true：</p>
<pre>
&lt;%@ page isELIgnored ="true|false" %&gt;
</pre>
<p>
这样，EL表达式就会被忽略。若设为false，则容器将会计算EL表达式。</p><hr>
<h2>
	EL中的基础操作符</h2><p>
EL表达式支持大部分Java所提供的算术和逻辑操作符：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>操作符</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				.</td>
			<td>
				访问一个Bean属性或者一个映射条目</td>
		</tr>
		<tr>
			<td>
				[]</td>
			<td>
				访问一个数组或者链表的元素</td>
		</tr>
		<tr>
			<td>
				( )</td>
			<td>
				组织一个子表达式以改变优先级</td>
		</tr>
		<tr>
			<td>
				+</td>
			<td>
				加</td>
		</tr>
		<tr>
			<td>
				-</td>
			<td>
				减或负</td>
		</tr>
		<tr>
			<td>
				*</td>
			<td>
				乘</td>
		</tr>
		<tr>
			<td>
				/ or div</td>
			<td>
				除</td>
		</tr>
		<tr>
			<td>
				% or mod</td>
			<td>
				取模</td>
		</tr>
		<tr>
			<td>
				== or eq</td>
			<td>
				测试是否相等</td>
		</tr>
		<tr>
			<td>
				!= or ne</td>
			<td>
				测试是否不等</td>
		</tr>
		<tr>
			<td>
				&lt; or lt</td>
			<td>
				测试是否小于</td>
		</tr>
		<tr>
			<td>
				&gt; or gt</td>
			<td>
				测试是否大于</td>
		</tr>
		<tr>
			<td>
				&lt;= or le</td>
			<td>
				测试是否小于等于</td>
		</tr>
		<tr>
			<td>
				&gt;= or ge</td>
			<td>
				测试是否大于等于</td>
		</tr>
		<tr>
			<td>
				&amp;&amp; or and</td>
			<td>
				测试逻辑与</td>
		</tr>
		<tr>
			<td>
				|| or or</td>
			<td>
				测试逻辑或</td>
		</tr>
		<tr>
			<td>
				! or not</td>
			<td>
				测试取反</td>
		</tr>
		<tr>
			<td>
				empty</td>
			<td>
				测试是否空值</td>
		</tr>
	</tbody>
</table><hr>
<h2>
	JSP EL中的函数</h2><p>
JSP EL允许您在表达式中使用函数。这些函数必须被定义在自定义标签库中。函数的使用语法如下：</p>
<pre>
${ns:func(param1, param2, ...)}
</pre>
<p>
ns指的是命名空间（namespace），func指的是函数的名称，param1指的是第一个参数，param2指的是第二个参数，以此类推。比如，有函数fn:length，在JSTL库中定义，可以像下面这样来获取一个字符串的长度：
</p>
<pre>
${fn:length("Get my length")}
</pre><p>
要使用任何标签库中的函数，您需要将这些库安装在服务器中，然后使用&lt;taglib&gt;标签在JSP文件中包含这些库。</p><hr>
<h2>
	JSP EL隐含对象</h2><p>
JSP EL支持下表列出的隐含对象：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>隐含对象</strong></th>
			<th>
				<strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				pageScope</td>
			<td>
				page 作用域</td>
		</tr>
		<tr>
			<td>
				requestScope</td>
			<td>
				request 作用域</td>
		</tr>
		<tr>
			<td>
				sessionScope</td>
			<td>
				session 作用域</td>
		</tr>
		<tr>
			<td>
				applicationScope</td>
			<td>
				application 作用域</td>
		</tr>
		<tr>
			<td>
				param</td>
			<td>
				Request 对象的参数，字符串</td>
		</tr>
		<tr>
			<td>
				paramValues</td>
			<td>
				Request对象的参数，字符串集合</td>
		</tr>
		<tr>
			<td>
				header</td>
			<td>
				HTTP 信息头，字符串</td>
		</tr>
		<tr>
			<td>
				headerValues</td>
			<td>
				HTTP 信息头，字符串集合</td>
		</tr>
		<tr>
			<td>
				initParam</td>
			<td>
				上下文初始化参数</td>
		</tr>
		<tr>
			<td>
				cookie</td>
			<td>
				Cookie值</td>
		</tr>
		<tr>
			<td>
				pageContext</td>
			<td>
				当前页面的pageContext</td>
		</tr>
	</tbody>
</table>
<p>
您可以在表达式中使用这些对象，就像使用变量一样。接下来会给出几个例子来更好的理解这个概念。</p><hr>
<h2>
	pageContext对象</h2><p>
pageContext对象是JSP中pageContext对象的引用。通过pageContext对象，您可以访问request对象。比如，访问request对象传入的查询字符串，就像这样：</p>
<pre>
${pageContext.request.queryString}
</pre><hr>
<h2>
	Scope对象</h2><p>
pageScope，requestScope，sessionScope，applicationScope变量用来访问存储在各个作用域层次的变量。</p>
<p>
举例来说，如果您需要显式访问在applicationScope层的box变量，可以这样来访问：applicationScope.box。</p><hr>
<h2>
	param和paramValues对象</h2><p>
param和paramValues对象用来访问参数值，通过使用request.getParameter方法和request.getParameterValues方法。</p>
<p>
举例来说，访问一个名为order的参数，可以这样使用表达式：${param.order}，或者${param["order"]}。</p>
<p>
接下来的例子表明了如何访问request中的username参数：</p>
<pre>
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;%
    String title = "Accessing Request Param";
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;% out.print(title); %&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;&lt;% out.print(title); %&gt;&lt;/h1&gt;
&lt;/center&gt;
&lt;div align="center"&gt;
&lt;p&gt;${param["username"]}&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
param对象返回单一的字符串，而paramValues对象则返回一个字符串数组。</p>
<hr>
<h2>
	header和headerValues对象</h2><p>
header和headerValues对象用来访问信息头，通过使用 request.getHeader方法和request.getHeaders方法。</p>
<p>
举例来说，要访问一个名为user-agent的信息头，可以这样使用表达式：${header.user-agent}，或者${header["user-agent"]}。</p>
<p>
接下来的例子表明了如何访问user-agent信息头：</p>
<pre>
&lt;%@ page import="java.io.*,java.util.*" %&gt;
&lt;%
    String title = "User Agent Example";
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;% out.print(title); %&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;&lt;% out.print(title); %&gt;&lt;/h1&gt;
&lt;/center&gt;
&lt;div align="center"&gt;
&lt;p&gt;${header["user-agent"]}&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
运行结果如下：</p>
<p><img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp-expression-language.jpg" alt="jsp-expression-language" />
</p><p>
header对象返回单一值，而headerValues则返回一个字符串数组。</p>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-custom-tags.html" rel="prev"> JSP 自定义标签</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-exception-handling.html" rel="next"> JSP 异常处理</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-expression-language.html" rel="prev"> JSP 表达式语言</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-debugging.html" rel="next"> JSP 调试</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">异常处理</span></h1>
<p>当编写JSP程序的时候，程序员可能会遗漏一些BUG，这些BUG可能会出现在程序的任何地方。JSP代码中通常有以下几类异常:</p>
<ul>
<li>
检查型异常:检查型异常就是一个典型的用户错误或者一个程序员无法预见的错误。举例来说，如果一个文件将要被打开，但是无法找到这个文件，则一个异常被抛出。这些异常不能再编译期被简单地忽略。</li>
<li>
运行时异常:一个运行时异常可能已经被程序员避免，这种异常在编译期将会被忽略。</li>
<li>
错误:这里没有异常，但问题是它超出了用户或者程序员的控制范围。错误通常会在代码中被忽略，您几乎不能拿它怎么样。举例来或，栈溢出错误。这些错误都会在编译期被忽略。</li>
</ul><p>
本节将会给出几个简单而优雅的方式来处理运行时异常和错误。</p><hr>
<h2>
使用Exception对象</h2><p>
exception对象是Throwable子类的一个实例，只在错误页面中可用。下表列出了Throwable类中一些重要的方法:</p>
<table class="reference">
<tbody>
<tr>
<th>
<strong>序号</strong></th>
<th>
<strong>方法</strong><strong>&amp;</strong><strong>描述</strong></th>
</tr>
<tr>
<td>
1</td>
<td>
<strong>public String getMessage()</strong></p>
返回异常的信息。这个信息在Throwable构造函数中被初始化</td>
</tr>
<tr>
<td>
2</td>
<td>
<strong>public ThrowablegetCause()</strong></p>
返回引起异常的原因，类型为Throwable对象</td>
</tr>
<tr>
<td>
3</td>
<td>
<strong>public String toString()</strong></p>
返回类名</td>
</tr>
<tr>
<td>
4</td>
<td>
<strong>public void printStackTrace()</strong></p>
将异常栈轨迹输出至System.err</td>
</tr>
<tr>
<td>
5</td>
<td>
<strong>public StackTraceElement [] getStackTrace()</strong></p>
以栈轨迹元素数组的形式返回异常栈轨迹</td>
</tr>
<tr>
<td>
6</td>
<td>
<strong>public ThrowablefillInStackTrace()</strong></p>
使用当前栈轨迹填充Throwable对象</td>
</tr>
</tbody>
</table>
<p>
JSP提供了可选项来为每个JSP页面指定错误页面。无论何时页面抛出了异常，JSP容器都会自动地调用错误页面。</p>
<p>
接下来的例子为main.jsp指定了一个错误页面。使用&lt;%@page errorPage="XXXXX"%&gt;指令指定一个错误页面。</p>
<pre>
&lt;%@ page errorPage="ShowError.jsp" %&gt;

&lt;html&gt;
&lt;head&gt;
   &lt;title&gt;Error Handling Example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
   // Throw an exception to invoke the error page
   int x = 1;
   if (x == 1)
   {
      throw new RuntimeException("Error condition!!!");
   }
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
现在，编写ShowError.jsp文件如下:</p>
<pre>
&lt;%@ page isErrorPage="true" %&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Show Error Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Opps...&lt;/h1&gt;
&lt;p&gt;Sorry, an error occurred.&lt;/p&gt;
&lt;p&gt;Here is the exception stack trace: &lt;/p&gt;
&lt;pre&gt;
&lt;% exception.printStackTrace(response.getWriter()); %&gt;
</pre>
</body>
</html>
</pre><p>
注意到，ShowError.jsp文件使用了&lt;%@page isErrorPage="true"%&gt;指令，这个指令告诉JSP编译器需要产生一个异常实例变量。</p>
<p>
现在试着访问main.jsp页面，它将会产生如下结果:</p>
<pre>
java.lang.RuntimeException: Error condition!!!
......

Opps...
Sorry, an error occurred.

Here is the exception stack trace:
</pre><hr>
<h2>
在错误页面中使用JSTL标签</h2><p>
可以利用JSTL标签来编写错误页面ShowError.jsp。这个例子中的代码与上例代码的逻辑几乎一样，但是本例的代码有更好的结构，并且能够提供更多信息:</p>
<pre>
&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
&lt;%@page isErrorPage="true" %&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Show Error Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Opps...&lt;/h1&gt;
&lt;table width="100%" border="1"&gt;
&lt;tr valign="top"&gt;
&lt;td width="40%"&gt;&lt;b&gt;Error:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;${pageContext.exception}&lt;/td&gt;
&lt;/tr&gt;
&lt;tr valign="top"&gt;
&lt;td&gt;&lt;b&gt;URI:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;${pageContext.errorData.requestURI}&lt;/td&gt;
&lt;/tr&gt;
&lt;tr valign="top"&gt;
&lt;td&gt;&lt;b&gt;Status code:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;${pageContext.errorData.statusCode}&lt;/td&gt;
&lt;/tr&gt;
&lt;tr valign="top"&gt;
&lt;td&gt;&lt;b&gt;Stack trace:&lt;/b&gt;&lt;/td&gt;
&lt;td&gt;
&lt;c:forEach var="trace" 
         items="${pageContext.exception.stackTrace}"&gt;
&lt;p&gt;${trace}&lt;/p&gt;
&lt;/c:forEach&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
运行结果如下:</p>
<p>
<img src="http://www.runoob.com/wp-content/uploads/2014/01/jsp-exeception-1.jpg" alt="jsp-exeception-1" width="551" height="305" class="alignnone size-full wp-image-5223" srcset="http://www.runoob.com/wp-content/uploads/2014/01/jsp-exeception-1.jpg 551w, http://www.runoob.com/wp-content/uploads/2014/01/jsp-exeception-1-300x166.jpg 300w" sizes="(max-width: 551px) 100vw, 551px" />
</p><hr>
<h2>
使用 try…catch块</h2><p>
如果您想要将异常处理放在一个页面中，并且对不同的异常进行不同的处理，那么您就需要使用try…catch块了。</p>
<p>
接下来的这个例子显示了如何使用try…catch块，将这些代码放在main.jsp中:</p>
<pre>
&lt;html&gt;
&lt;head&gt;
   &lt;title&gt;Try...Catch Example&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
   try{
      int i = 1;
      i = i / 0;
      out.println("The answer is " + i);
   }
   catch (Exception e){
      out.println("An exception occurred: " + e.getMessage());
   }
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
试着访问main.jsp，它将会产生如下结果:</p>
<pre>
An exception occurred: / by zero 
</pre>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-expression-language.html" rel="prev"> JSP 表达式语言</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-debugging.html" rel="next"> JSP 调试</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-exception-handling.html" rel="prev"> JSP 异常处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-internationalization.html" rel="next"> JSP 国际化</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">调试</span></h1>
<p>要测试/调试一个JSP或servlet程序总是那么的难。JSP和Servlets程序趋向于牵涉到大量客户端/服务器之间的交互，这很有可能会产生错误，并且很难重现出错的环境。</p>
<p>
接下来将会给出一些小技巧和小建议，来帮助您调试程序。</p><hr>
<h2>
	使用System.out.println()</h2><p>
System.out.println()可以很方便地标记一段代码是否被执行。当然，我们也可以打印出各种各样的值。此外：</p>
<ul>
	<li>
		自从System对象成为Java核心对象后，它便可以使用在任何地方而不用引入额外的类。使用范围包括Servlets，JSP，RMI，EJB's，Beans，类和独立应用。</li>
	<li>
		与在断点处停止运行相比，用System.out进行输出不会对应用程序的运行流程造成重大的影响，这个特点在定时机制非常重要的应用程序中就显得非常有用了。</li>
</ul><p>
接下来给出了使用System.out.println()的语法：</p>
<pre>
System.out.println("Debugging message");
</pre>
<p>
这是一个使用System.out.print()的简单例子：</p>
<pre>
&lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
&lt;html&gt;
&lt;head&gt;&lt;title&gt;System.out.println&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;c:forEach var="counter" begin="1" end="10" step="1" &gt;
   &lt;c:out value="${counter-5}"/&gt;&lt;/br&gt;
   &lt;% System.out.println( "counter= " + 
                     pageContext.findAttribute("counter") ); %&gt;
&lt;/c:forEach&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><p>
现在，如果运行上面的例子的话，它将会产生如下的结果：</p>
<pre>
-4
-3
-2
-1
0
1
2
3
4
5
</pre>
<p>
如果使用的是Tomcat服务器，您就能够在logs目录下的stdout.log文件中发现多出了如下内容：</p>
<pre>
counter=1
counter=2
counter=3
counter=4
counter=5
counter=6
counter=7
counter=8
counter=9
counter=10
</pre>
<p>
使用这种方法可以将变量和其它的信息输出至系统日志中，用来分析并找出造成问题的深层次原因。</p><hr>
<h2>
	使用JDB Logger</h2><p>
J2SE日志框架可为任何运行在JVM中的类提供日志记录服务。因此我们可以利用这个框架来记录任何信息。</p>
<p>
让我们来重写以上代码，使用JDK中的 logger API：</p>
<pre>
&lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
&lt;%@page import="java.util.logging.Logger" %&gt;

&lt;html&gt;
&lt;head&gt;&lt;title&gt;Logger.info&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;% Logger logger=Logger.getLogger(this.getClass().getName());%&gt;

&lt;c:forEach var="counter" begin="1" end="10" step="1" &gt;
   &lt;c:set var="myCount" value="${counter-5}" /&gt;
   &lt;c:out value="${myCount}"/&gt;&lt;/br&gt;
   &lt;% String message = "counter="
                  + pageContext.findAttribute("counter")
                  + " myCount="
                  + pageContext.findAttribute("myCount");
                  logger.info( message );
   %&gt;
&lt;/c:forEach&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>
它的运行结果与先前的类似，但是，它可以获得额外的信息输出至stdout.log文件中。在这我们使用了logger中的info方法。下面我们给出stdout.log文件中的一个快照：</p>
<pre>
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=1 myCount=-4
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=2 myCount=-3
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=3 myCount=-2
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=4 myCount=-1
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=5 myCount=0
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=6 myCount=1
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=7 myCount=2
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=8 myCount=3
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=9 myCount=4
24-Sep-2013 23:31:31 org.apache.jsp.main_jsp _jspService
INFO: counter=10 myCount=5
</pre>
<p>
消息可以使用各种优先级发送，通过使用sever()，warning()，info()，config()，fine()，finer()，finest()方法。finest()方法用来记录最好的信息，而sever()方法用来记录最严重的信息。</p>
<p>
使用Log4J 框架来将消息记录在不同的文件中，这些消息基于严重程度和重要性来进行分类。</p><hr>
<h2>
	调试工具</h2><p>
NetBeans是树形结构，是开源的Java综合开发环境，支持开发独立的Java应用程序和网络应用程序，同时也支持JSP调试。</p>
<p>
NetBeans支持如下几个基本的调试功能：</p>
<ul>
	<li>
		断点</li>
	<li>
		单步跟踪</li>
	<li>
		观察点</li>
</ul><p>
详细的信息可以查看NetBeans使用手册。</p><hr>
<h2>
	使用JDB Debugger</h2><p>
可以在JSP和servlets中使用jdb命令来进行调试，就像调试普通的应用程序一样。</p>
<p>
通常，我们直接调试sun.servlet.http.HttpServer 对象来查看HttpServer在响应HTTP请求时执行JSP/Servlets的情况。这与调试applets非常相似。不同之处在于，applets程序实际调试的是sun.applet.AppletViewer。</p>
<p>
大部分调试器在调试applets时都能够自动忽略掉一些细节，因为它知道如何调试applets。如果想要将调试对象转移到JSP身上，就需要做好以下两点：</p>
<ul>
	<li>
		设置调试器的classpath，让它能够找到sun.servlet.http.Http-Server&nbsp; 和相关的类。</li>
	<li>
		设置调试器的classpath，让它能够找到您的JSP文件和相关的类。</li>
</ul><p>
设置好classpath后，开始调试sun.servlet.http.Http-Server 。您可以在JSP文件的任意地方设置断点，只要你喜欢，然后使用浏览器发送一个请求给服务器就应该可以看见程序停在了断点处。</p>
<hr><h2>
	使用注释</h2><p>
程序中的注释在很多方面都对程序的调试起到一定的帮助作用。注释可以用在调试程序的很多方面中。</p>
<p>
JSP使用Java注释。如果一个BUG消失了，就请仔细查看您刚注释过的代码，通常都能找出原因。</p><hr>
<h2>
	客户端和服务器的头模块</h2><p>
有时候，当JSP没有按照预定的方式运行时，查看未加工的HTTP请求和响应也是很有用的。如果对HTTP的结构很熟悉的话，您可以直接观察request和response然后看看这些头模块到底怎么了。</p>
<hr><h2>
	重要调试技巧</h2><p>
这里我们再透露两个调试JSP的小技巧：</p>
<ul>
	<li>
		使用浏览器显示原始的页面内容，用来区分是否是格式问题。这个选项通常在View菜单下。</li>
	<li>
		确保浏览器在强制重新载入页面时没有捕获先前的request输出。若使用的是Netscape Navigator浏览器，则用Shift-Reload；若使用的是IE浏览器，则用Shift-Refresh。</li>
</ul>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-exception-handling.html" rel="prev"> JSP 异常处理</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jsp-internationalization.html" rel="next"> JSP 国际化</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-unique.html" title="jQuery.unique()  方法">jQuery.unique()...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-type.html" title="jQuery.type()   方法">jQuery.type()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-trim.html" title="jQuery.trim()  方法">jQuery.trim()  ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsexml.html" title="jQuery.parseXML()  方法">jQuery.parseXML...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsejson.html" title="jQuery.parseJSON()  方法">jQuery.parseJSO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-parsehtml.html" title="jQuery.parseHTML()  方法">jQuery.parseHTM...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-now.html" title="jQuery.now() 方法">jQuery.now() 方法</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" value="搜索……" name="s" autocomplete="off">
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="/">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        <div class="col search search-desktop last">
      <form action="http://www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" value="搜索……"  autocomplete="off">
      </form>
    </div>
  </div>
</div>


<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<li><a href="/jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="/bootstrap/bootstrap-tutorial.html">Bootstrap</a></li>
				<li><a href="/sql/sql-tutorial.html">SQL</a></li>
				<li><a href="/mysql/mysql-tutorial.html">MySQL</a></li>
				<li><a href="/php/php-tutorial.html">PHP</a></li>
				<li><a href="/python/python-tutorial.html">Python</a></li>
				<li><a href="/cprogramming/c-tutorial.html">C</a></li>
				<li><a href="/cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="/csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="/java/java-tutorial.html">Java</a></li>
				<li><a href="/ruby/ruby-tutorial.html">Ruby</a></li>
				
				<li><a href="/sitemap">更多……</a></li>
      		</ul>
			<ul class="mobile-nav">
				<li><a href="http://www.runoob.com/">首页</a></li>
				<li><a href="/html/html-tutorial.html">HTML</a></li>
				<li><a href="/css/css-tutorial.html">CSS</a></li>
				<li><a href="/js/js-tutorial.html">JavaScript</a></li>
				<a href="javascript:void(0)" class="search-reveal">Search</a> 
			</ul>
			
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="col left-column">
	<div class="tab">JSP 教程</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="JSP 教程"  href="/jsp/jsp-tutorial.html" >
			JSP 教程			</a>
						<a target="_top" title="JSP 简介"  href="/jsp/jsp-intro.html" >
			JSP 简介			</a>
						<a target="_top" title="JSP 开发环境搭建"  href="/jsp/jsp-setup.html" >
			JSP 开发环境搭建			</a>
			<a target="_top" title="Eclipse JSP/Servlet" href="/jsp/eclipse-jsp.html"> Eclipse JSP/Servlet</a>			<a target="_top" title="JSP 结构"  href="/jsp/jsp-architecture.html" >
			JSP 结构			</a>
						<a target="_top" title="JSP 生命周期"  href="/jsp/jsp-life-cycle.html" >
			JSP 生命周期			</a>
						<a target="_top" title="JSP 语法"  href="/jsp/jsp-syntax.html" >
			JSP 语法			</a>
						<a target="_top" title="JSP 指令"  href="/jsp/jsp-directives.html" >
			JSP 指令			</a>
						<a target="_top" title="JSP 动作元素"  href="/jsp/jsp-actions.html" >
			JSP 动作元素			</a>
						<a target="_top" title="JSP 隐式对象"  href="/jsp/jsp-implicit-objects.html" >
			JSP 隐式对象			</a>
						<a target="_top" title="JSP 客户端请求"  href="/jsp/jsp-client-request.html" >
			JSP 客户端请求			</a>
						<a target="_top" title="JSP 服务器响应"  href="/jsp/jsp-server-response.html" >
			JSP 服务器响应			</a>
						<a target="_top" title="JSP HTTP 状态码"  href="/jsp/jsp-http-status-codes.html" >
			JSP HTTP 状态码			</a>
						<a target="_top" title="JSP 表单处理"  href="/jsp/jsp-form-processing.html" >
			JSP 表单处理			</a>
						<a target="_top" title="JSP 过滤器"  href="/jsp/jsp-writing-filters.html" >
			JSP 过滤器			</a>
						<a target="_top" title="JSP Cookie 处理"  href="/jsp/jsp-cookies.html" >
			JSP Cookie 处理			</a>
						<a target="_top" title="JSP Session"  href="/jsp/jsp-session.html" >
			JSP Session			</a>
						<a target="_top" title="JSP 文件上传"  href="/jsp/jsp-file-uploading.html" >
			JSP 文件上传			</a>
						<a target="_top" title="JSP 日期处理"  href="/jsp/jsp-handling-date.html" >
			JSP 日期处理			</a>
						<a target="_top" title="JSP 页面重定向"  href="/jsp/jsp-page-redirect.html" >
			JSP 页面重定向			</a>
						<a target="_top" title="JSP 点击量统计"  href="/jsp/jsp-hits-counter.html" >
			JSP 点击量统计			</a>
						<a target="_top" title="JSP 自动刷新"  href="/jsp/jsp-auto-refresh.html" >
			JSP 自动刷新			</a>
						<a target="_top" title="JSP 发送邮件"  href="/jsp/jsp-sending-email.html" >
			JSP 发送邮件			</a>
			<br><h2 class="left"><span class="left_h2">JSP 高级教程</span></h2>			<a target="_top" title="JSP 标准标签库（JSTL）"  href="/jsp/jsp-jstl.html" >
			JSP 标准标签库（JSTL）			</a>
						<a target="_top" title="JSP 连接数据库"  href="/jsp/jsp-database-access.html" >
			JSP 连接数据库			</a>
						<a target="_top" title="JSP XML 数据处理"  href="/jsp/jsp-xml-data.html" >
			JSP XML 数据处理			</a>
						<a target="_top" title="JSP JavaBean"  href="/jsp/jsp-javabean.html" >
			JSP JavaBean			</a>
						<a target="_top" title="JSP 自定义标签"  href="/jsp/jsp-custom-tags.html" >
			JSP 自定义标签			</a>
						<a target="_top" title="JSP 表达式语言"  href="/jsp/jsp-expression-language.html" >
			JSP 表达式语言			</a>
						<a target="_top" title="JSP 异常处理"  href="/jsp/jsp-exception-handling.html" >
			JSP 异常处理			</a>
						<a target="_top" title="JSP 调试"  href="/jsp/jsp-debugging.html" >
			JSP 调试			</a>
						<a target="_top" title="JSP 国际化"  href="/jsp/jsp-internationalization.html" >
			JSP 国际化			</a>
				
		</div>
	</div>	
</div>	<div class="col middle-column">
		
	
	<div class="article">
			<div class="article-heading-ad">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 移动版 自动调整 -->
		<ins class="adsbygoogle"
		     style="display:inline-block;min-width:300px;max-width:970px;width:100%;height:90px"
		     data-ad-client="ca-pub-5751451760833794"
		     data-ad-slot="1691338467"
		     data-ad-format="horizontal"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-debugging.html" rel="prev"> JSP 调试</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jstl-core-out-tag.html" rel="next"> &lt;c:out&gt; 标签</a> →</div>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>JSP <span class="color_h1">国际化</span></h1>
<p>在开始前，需要解释几个重要的概念：</p>
<ul>
	<li>
		国际化（i18n）：表明一个页面根据访问者的语言或国家来呈现不同的翻译版本。</li>
	<li>
		本地化（l10n）：向网站添加资源，以使它适应不同的地区和文化。比如网站的印度语版本。</li>
	<li>
		区域：这是一个特定的区域或文化，通常认为是一个语言标志和国家标志通过下划线连接起来。比如"en_US"代表美国英语地区。</li>
</ul><p>
如果想要建立一个全球化的网站，就需要关心一系列项目。本章将会详细告诉您如何处理国际化问题，并给出了一些例子来加深理解。</p>
<p>
JSP容器能够根据request的locale属性来提供正确地页面版本。接下来给出了如何通过request对象来获得Locale对象的语法：</p>
<pre>
java.util.Locale request.getLocale() 
</pre><hr>
<h2>
	检测Locale</h2><p>
下表列举出了Locale对象中比较重要的方法，用于检测request对象的地区，语言，和区域。所有这些方法都会在浏览器中显示国家名称和语言名称：</p>
<table class="reference">
	<tbody>
		<tr>
			<th>
				<strong>序号</strong></th>
			<th>
				<strong>方法</strong><strong> &amp; </strong><strong>描述</strong></th>
		</tr>
		<tr>
			<td>
				1</td>
			<td>
				<strong>String getCountry()</strong></p>
				返回国家/地区码的英文大写，或 ISO 3166 2-letter 格式的区域</td>
		</tr>
		<tr>
			<td>
				2</td>
			<td>
				<strong>String getDisplayCountry()</strong></p>
				返回要显示给用户的国家名称</td>
		</tr>
		<tr>
			<td>
				3</td>
			<td>
				<strong>String getLanguage()</strong></p>
				返回语言码的英文小写，或ISO 639 格式的区域</td>
		</tr>
		<tr>
			<td>
				4</td>
			<td>
				<strong>String getDisplayLanguage()</strong></p>
				返回要给用户看的语言名称</td>
		</tr>
		<tr>
			<td>
				5</td>
			<td>
				<strong>String getISO3Country()</strong></p>
				返回国家名称的3字母缩写</td>
		</tr>
		<tr>
			<td>
				6</td>
			<td>
				<strong>String getISO3Language()</strong></p>
				返回语言名称的3字母缩写</td>
		</tr>
	</tbody>
</table><hr>
<h2>实例演示</h2><p>
这个例子告诉我们如何在JSP中显示语言和国家：</p>
<pre>
&lt;%@ page import="java.io.*,java.util.Locale" %&gt;
&lt;%@ page import="javax.servlet.*,javax.servlet.http.* "%&gt;
&lt;%
   //获取客户端本地化信息
   Locale locale = request.getLocale();
   String language = locale.getLanguage();
   String country = locale.getCountry();
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Detecting Locale&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;Detecting Locale&lt;/h1&gt;
&lt;/center&gt;
&lt;p align="center"&gt;
&lt;% 
   out.println("Language : " + language  + "&lt;br /&gt;");
   out.println("Country  : " + country   + "&lt;br /&gt;");
%&gt;
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><hr>
<h2>
	语言设置</h2><p>
JSP可以使用西欧语言来输出一个页面，比如英语，西班牙语，德语，法语，意大利语等等。由此可见，设置Content-Language信息头来正确显示所有字符是很重要的。</p>
<p>
第二点就是，需要使用HTML字符实体来显示特殊字符，比如"&amp;#241;" 代表的是"?"，"&amp;#161;"代表的是 "?" ：</p>
<pre>
&lt;%@ page import="java.io.*,java.util.Locale" %&gt;
&lt;%@ page import="javax.servlet.*,javax.servlet.http.* "%&gt;
&lt;%
    // Set response content type
    response.setContentType("text/html");
    // Set spanish language code.
    response.setHeader("Content-Language", "es");
    String title = "En Espa?ol";

%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;%  out.print(title); %&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;&lt;%  out.print(title); %&gt;&lt;/h1&gt;
&lt;/center&gt;
&lt;div align="center"&gt;
&lt;p&gt;En Espa?ol&lt;/p&gt;
&lt;p&gt;?Hola Mundo!&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><hr>
<h2>
	区域特定日期</h2><p>
可以使用java.text.DateFormat类和它的静态方法getDateTimeInstance()来格式化日期和时间。接下来的这个例子显示了如何根据指定的区域来格式化日期和时间：</p>
<pre>
&lt;%@ page import="java.io.*,java.util.Locale" %&gt;
&lt;%@ page import="javax.servlet.*,javax.servlet.http.* "%&gt;
&lt;%@ page import="java.text.DateFormat,java.util.Date" %&gt;

&lt;%
    String title = "Locale Specific Dates";
    //Get the client's Locale
    Locale locale = request.getLocale( );
    String date = DateFormat.getDateTimeInstance(
                                  DateFormat.FULL, 
                                  DateFormat.SHORT, 
                                  locale).format(new Date( ));
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;% out.print(title); %&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;&lt;% out.print(title); %&gt;&lt;/h1&gt;
&lt;/center&gt;
&lt;div align="center"&gt;
&lt;p&gt;Local Date: &lt;%  out.print(date); %&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><hr>
<h2>
	区域特定货币</h2><p>
可以使用java.text.NumberFormat类和它的静态方法getCurrencyInstance()来格式化数字。比如在区域特定货币中的long型和double型。接下来的例子显示了如何根据指定的区域来格式化货币：</p>
<pre>
&lt;%@ page import="java.io.*,java.util.Locale" %&gt;
&lt;%@ page import="javax.servlet.*,javax.servlet.http.* "%&gt;
&lt;%@ page import="java.text.NumberFormat,java.util.Date" %&gt;

&lt;%
    String title = "Locale Specific Currency";
    //Get the client's Locale
    Locale locale = request.getLocale( );
    NumberFormat nft = NumberFormat.getCurrencyInstance(locale);
    String formattedCurr = nft.format(1000000);
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;% out.print(title); %&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;&lt;% out.print(title); %&gt;&lt;/h1&gt;
&lt;/center&gt;
&lt;div align="center"&gt;
&lt;p&gt;Formatted Currency: &lt;%  out.print(formattedCurr); %&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre><hr>
<h2>
	区域特定百分比</h2><p>
可以使用java.text.NumberFormat类和它的静态方法getPercentInstance()来格式化百分比。接下来的例子告诉我们如何根据指定的区域来格式化百分比：</p>
<pre>
&lt;%@ page import="java.io.*,java.util.Locale" %&gt;
&lt;%@ page import="javax.servlet.*,javax.servlet.http.* "%&gt;
&lt;%@ page import="java.text.NumberFormat,java.util.Date" %&gt;

&lt;%
    String title = "Locale Specific Percentage";
    //Get the client's Locale
    Locale locale = request.getLocale( );
    NumberFormat nft = NumberFormat.getPercentInstance(locale);
    String formattedPerc = nft.format(0.51);
%&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;% out.print(title); %&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;h1&gt;&lt;% out.print(title); %&gt;&lt;/h1&gt;
&lt;/center&gt;
&lt;div align="center"&gt;
&lt;p&gt;Formatted Percentage: &lt;%  out.print(formattedPerc); %&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>			
			
			</div>
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link">← <a href="http://www.runoob.com/jsp/jsp-debugging.html" rel="prev"> JSP 调试</a> </div>
			<div class="next-design-link"><a href="http://www.runoob.com/jsp/jstl-core-out-tag.html" rel="next"> &lt;c:out&gt; 标签</a> →</div>
		</div>
		
		<div class="sidebar-box ad-box ad-box-large">
		<div class="ad-336280">
			<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
			<!-- 移动版 自动调整 -->
			<ins class="adsbygoogle"
			     style="display:inline-block;min-width:200px;max-width:360px;width:100%;height:100%px"
			     data-ad-client="ca-pub-5751451760833794"
			     data-ad-slot="1691338467"
			     data-ad-format="rectangle"></ins>
			<script>
			(adsbygoogle = window.adsbygoogle || []).push({});
			</script>
		</div>
		</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">
	<div class="tab tab-light-blue" style="text-align: center;">关注微信</div>
	<div class="sidebar-box">
		<a href="http://m.runoob.com/" target="_blank"> <img src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" alt="移动版"> </a>
		<div class="qqinfo">
		<!-- <a target="_blank" href="http://jq.qq.com/?_wv=1027&k=dOwwKN" id="qqhref">
		<img border="0" src="http://pub.idqqimg.com/wpa/images/group.png" alt="菜鸟家族" title="菜鸟家族"></a>
		<span>(群号：<span id="qqid">365967760</span>)</span>-->
		</div>
		
	</div>
	

	
	<div class="tab tab-light-blue">分类导航</div>
	<div class="sidebar-box sidebar-cate">
		
		<div class="sidebar-tree" >
			<ul><li><a href="javascript:void(0);" class="tit">HTML / CSS</a><ul><li><a title="HTML 教程" href="http://www.runoob.com/html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="http://www.runoob.com/html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="http://www.runoob.com/css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="http://www.runoob.com/css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap 教程" href="http://www.runoob.com/bootstrap/bootstrap-tutorial.html">Bootstrap 教程</a></li><li><a title="Foundation 教程" href="http://www.runoob.com/foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">JavaScript</a><ul><li><a title="JavaScript 教程" href="http://www.runoob.com/js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="http://www.runoob.com/htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="http://www.runoob.com/jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="http://www.runoob.com/angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="React 教程" href="http://www.runoob.com/react/react-tutorial.html">React 教程</a></li><li><a title="jQuery UI 教程" href="http://www.runoob.com/jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="http://www.runoob.com/jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="http://www.runoob.com/nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="http://www.runoob.com/ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="http://www.runoob.com/json/json-tutorial.html">JSON 教程</a></li><li><a title="Highcharts 教程" href="http://www.runoob.com/highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="http://www.runoob.com/googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">服务端</a><ul><li><a title="PHP 教程" href="http://www.runoob.com/php/php-tutorial.html">PHP 教程</a></li><li><a title="Python 教程" href="http://www.runoob.com/python/python-tutorial.html">Python 教程</a></li><li><a title="Python3 教程" href="http://www.runoob.com/python3/python3-tutorial.html">Python3 教程</a></li><li><a title="Django 教程" href="http://www.runoob.com/django/django-tutorial.html">Django 教程</a></li><li><a title="Linux 教程" href="http://www.runoob.com/linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="http://www.runoob.com/docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="http://www.runoob.com/ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="http://www.runoob.com/java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="http://www.runoob.com/c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="http://www.runoob.com/cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="http://www.runoob.com/perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="http://www.runoob.com/servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="http://www.runoob.com/jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="http://www.runoob.com/lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Scala 教程" href="http://www.runoob.com/scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="http://www.runoob.com/go/go-tutorial.html">Go 教程</a></li><li><a title="设计模式" href="http://www.runoob.com/design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="http://www.runoob.com/regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="ASP 教程" href="http://www.runoob.com/asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="http://www.runoob.com/appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="http://www.runoob.com/vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">数据库</a><ul><li><a title="SQL 教程" href="http://www.runoob.com/sql/sql-tutorial.html">SQL 教程</a></li><li><a title="Mysql 教程" href="http://www.runoob.com/mysql/mysql-tutorial.html">Mysql 教程</a></li><li><a title="SQLite 教程" href="http://www.runoob.com/sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="http://www.runoob.com/mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="http://www.runoob.com/redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="http://www.runoob.com/Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">移动端</a><ul><li><a title="Android 教程" href="http://www.runoob.com/w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="http://www.runoob.com/swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="http://www.runoob.com/jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="http://www.runoob.com/ionic/ionic-tutorial.html">ionic 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">XML 教程</a><ul><li><a title="XML 教程" href="http://www.runoob.com/xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="http://www.runoob.com/dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="http://www.runoob.com/dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="http://www.runoob.com/xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="http://www.runoob.com/xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="http://www.runoob.com/xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="http://www.runoob.com/xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="http://www.runoob.com/schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="http://www.runoob.com/xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="http://www.runoob.com/svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">ASP.NET</a><ul><li><a title="ASP.NET 教程" href="http://www.runoob.com/aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="http://www.runoob.com/csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="http://www.runoob.com/aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="http://www.runoob.com/aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="http://www.runoob.com/aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="http://www.runoob.com/aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">Web Services</a><ul><li><a title="Web Services 教程" href="http://www.runoob.com/webservices/webservices-tutorial.html">Web Services 教程</a></li><li><a title="WSDL 教程" href="http://www.runoob.com/wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="http://www.runoob.com/soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="http://www.runoob.com/rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="http://www.runoob.com/rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">开发工具</a><ul><li><a title="Eclipse 教程" href="http://www.runoob.com/eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="http://www.runoob.com/git/git-tutorial.html">Git 教程</a></li><li><a title="Firebug 教程" href="http://www.runoob.com/firebug/firebug-tutorial.html">Firebug 教程</a></li></ul></li><li><a href="javascript:void(0);" class="tit">网站建设</a><ul><li><a title="HTTP 教程" href="http://www.runoob.com/http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="http://www.runoob.com/web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="http://www.runoob.com/browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="http://www.runoob.com/hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="http://www.runoob.com/tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="http://www.runoob.com/w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="http://www.runoob.com/quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>			</div>
	
	</div>
	
	<br>
	
	<div class="sidebar-box ad-box ad-box-large">
		<div class="sidebar-box advertise-here">
			<a href="javascript:void(0);">Advertisement</a>
		</div>
		<div class="ad-600160">
		<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
		<!-- 侧栏1 -->
		<ins class="adsbygoogle"
			 style="display:inline-block;width:160px;height:600px"
			 data-ad-client="ca-pub-5751451760833794"
			 data-ad-slot="4106274865"></ins>
		<script>
		(adsbygoogle = window.adsbygoogle || []).push({});
		</script>
		</div>
	</div>
</div></div>

</div>
<!-- 底部 -->


<div id="footer" class="mar-t50">
   <div class="w3cschoolcc-block">
    <div class="w3cschoolcc cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a href="/html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a href="/js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a href="/ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a href="/jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a href="/xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a href="/java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a href="/charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a href="/tags/ref-entities.html">HTML ISO-8859-1</a>
      </dd> 
      <dd>
       &middot; <a href="/tags/ref-symbols.html">HTML 实体符号</a>
      </dd>
      <dd>
       &middot; <a href="/tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a href="/jsontool">JSON 格式化工具</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-merge.html" title="jQuery.merge()  方法">jQuery.merge() ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-map.html" title="jQuery.map()   方法">jQuery.map()   ...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-makearray.html" title="jQuery.makeArray()  方法">jQuery.makeArra...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isxmldoc.html" title="jQuery.isXMLDoc()  方法">jQuery.isXMLDoc...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-iswindow.html" title="jQuery.isWindow()  方法">jQuery.isWindow...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isplainobject.html" title="jQuery.isPlainObject()   方法">jQuery.isPlainO...</a>
      </dd>
              <dd>
       &middot;
      <a href="http://www.runoob.com/jquery/misc-isnumeric.html" title="jQuery.isNumeric()  方法">jQuery.isNumeri...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
      </dd>
      <dd>
       &middot;
      <a href="/disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a href="/aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a href="/archives">文章归档</a>
      </dd>
      
     </dl>
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="150" height="150" src="http://www.runoob.com/wp-content/themes/w3cschool.cc/assets/img/qrcode.jpg" />
       </div>
     </div>
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2016    <strong><a href="http://www.runoob.com/" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="http://www.runoob.com/" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：闽ICP备15012807号-1
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&amp;email=ssbDyoOAgfLU3crf09venNHd3w" style="font-size: 12px;" title="Bug 反馈">反馈</a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="http://www.runoob.com/wp-content/themes/runoob/assets/img/qrcode.jpg"></div>
    </div>
  </div>
<div style="display:none;">
  <script src="http://s11.cnzz.com/stat.php?id=5578006&web_id=5578006" language="JavaScript"></script>
  <script>
  var _hmt = _hmt || [];
  (function() {
    var hm = document.createElement("script");
    hm.src = "//hm.baidu.com/hm.js?8e2a116daf0104a78d601f40a45c75b4";
    var s = document.getElementsByTagName("script")[0]; 
    s.parentNode.insertBefore(hm, s);
  })();
  </script>
</div>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/main.js?v=1.02"></script>
<script src="http://www.runoob.com/wp-content/themes/runoob/assets/js/hl/run_prettify.js?v=1.02"></script>
</body>
</html>